Javascript [0] 位于匹配语句末尾

发布于 2025-01-08 19:03:35 字数 171 浏览 3 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无所的.畏惧 2025-01-15 19:03:35

match 返回一个 字符串数组

如果没有全局标志,数组的组成如下:

  • 仅使用第一个匹配。
  • 第一个键 [0] 包含第一个完整匹配
  • 其他键包含第一个匹配中匹配的

示例:

var string = 'abc_123_abd_456';
var regexp = /_([0-9])/; // Matches an underscore, and groups a number
var match = string.match(regexp);
// match[0] = _1 (full match)
// match[1] = 1  (group)

当指定全局标志时,数组包含所有完全匹配:

var string = 'abc_123_abd_456';
var regexp = /_([0-9])/g; // Matches an underscore, and groups a number, GLOBAL
var match = string.match(regexp);
// match[0] = _1 (first full match)
// match[1] = _4 (second full match)

我建议查看:

match returns an array of strings.

Without the global flag, the array is composed as follows:

  • Only the first match is used.
  • The first key, [0], contains the full first match.
  • The other keys contains the matched groups within the first match.

Example:

var string = 'abc_123_abd_456';
var regexp = /_([0-9])/; // Matches an underscore, and groups a number
var match = string.match(regexp);
// match[0] = _1 (full match)
// match[1] = 1  (group)

When the global flag is specified, the array consists of all full matches:

var string = 'abc_123_abd_456';
var regexp = /_([0-9])/g; // Matches an underscore, and groups a number, GLOBAL
var match = string.match(regexp);
// match[0] = _1 (first full match)
// match[1] = _4 (second full match)

I recommend to have a look at:

深居我梦 2025-01-15 19:03:35

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.

伪心 2025-01-15 19:03:35

请参阅 RobW 的答案以获得完整且准确的参考。


match 方法返回一个数组。

match() 方法搜索正则表达式和字符串之间的匹配项,并返回匹配项。
此方法返回匹配数组,如果未找到匹配则返回 null。

[0] 用于仅返回第一个元素。

See RobW's answer for complete and accurate reference.


The match method returns an array.

The match() method searches for a match between a regular expression and a string, and returns the matches.
This method returns an array of matches, or null if no match is found.

The [0] is used to return only the first element.

美人如玉 2025-01-15 19:03:35

当您需要一个字符串时,通常使用 replace 而不是 match 更实用:

 var1 = "www.asite.page.co.uk";
 var2 = '.' + var1.replace(/.+?(\w*\.\w*)$/, '$1'); // .co.uk

顺便说一句,您的表达式对我来说看起来不正确。匹配 TLD 的更好方法是同时

(\.[a-z]+\.[a-z][a-z]|\.[a-z]{3,})$

匹配“.co.uk”和“.com”。例子:

tld = "site.co.uk".replace(/.+?(\.[a-z]+\.[a-z][a-z]|\.[a-z]{3,})$/, '$1'); // .co.uk
tld = "yyy.com".replace(/.+?(\.[a-z]+\.[a-z][a-z]|\.[a-z]{3,})$/, '$1'); // .com

When you need one single string, it's often practical to use replace rather than match:

 var1 = "www.asite.page.co.uk";
 var2 = '.' + var1.replace(/.+?(\w*\.\w*)$/, '$1'); // .co.uk

BTW, your expression doesn't look right to me. A better way to match TLD would be something like

(\.[a-z]+\.[a-z][a-z]|\.[a-z]{3,})$

which matches both '.co.uk' and '.com'. Example:

tld = "site.co.uk".replace(/.+?(\.[a-z]+\.[a-z][a-z]|\.[a-z]{3,})$/, '$1'); // .co.uk
tld = "yyy.com".replace(/.+?(\.[a-z]+\.[a-z][a-z]|\.[a-z]{3,})$/, '$1'); // .com
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文