Javascript [0] 位于匹配语句末尾
我有以下内容:
var1 = "www.asite.page.co.uk";
var2 = '.' + var1.match(/\w*\.\w*$/) [0];
我知道它正在尝试实现“.co.uk”,但我不知道 [0] 的作用。 是错误处理吗?
I have the following:
var1 = "www.asite.page.co.uk";
var2 = '.' + var1.match(/\w*\.\w*$/) [0];
I understand that it's trying to achieve ".co.uk", but I have no idea what the [0] does.
Is it error handling?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
match
返回一个 字符串数组。如果没有全局标志,数组的组成如下:
[0]
包含第一个完整匹配。示例:
当指定全局标志时,数组包含所有完全匹配:
我建议查看:
match
returns an array of strings.Without the global flag, the array is composed as follows:
[0]
, contains the full first match.Example:
When the global flag is specified, the array consists of all full matches:
I recommend to have a look at:
match 方法返回一个匹配数组,因此 [0] 获取该数组的第一个元素——即完全匹配。
请参阅匹配或Rob W 的回答以获得更完整的描述。
The match method returns an array of matches, so the [0] gets the first element of that array -- i.e. just the full match.
See the documentation for match or Rob W's answer for a more complete description.
请参阅 RobW 的答案以获得完整且准确的参考。
match 方法返回一个数组。
[0]
用于仅返回第一个元素。See RobW's answer for complete and accurate reference.
The match method returns an array.
The
[0]
is used to return only the first element.当您需要一个字符串时,通常使用
replace
而不是match
更实用:顺便说一句,您的表达式对我来说看起来不正确。匹配 TLD 的更好方法是同时
匹配“.co.uk”和“.com”。例子:
When you need one single string, it's often practical to use
replace
rather thanmatch
:BTW, your expression doesn't look right to me. A better way to match TLD would be something like
which matches both '.co.uk' and '.com'. Example: