简体   繁体   English

这个正则表达式在做什么?

[英]What is this regex doing?

I thought this regex was about turning words into an array, but I get null as a result我认为这个正则表达式是关于将单词转换为数组,但结果是 null

when I want to get the second word from "facebox otherword" with array[0]当我想用数组 [0] 从“facebox otherword”中获取第二个单词时

This is the regex:这是正则表达式:

this.rel.match(/facebox\[?\.(\w+)\]?/)

Thanks Richard谢谢理查德

http://xenon.stanford.edu/~xusch/regexp/analyzer.html is a simple regex analyzer (among many others). http://xenon.stanford.edu/~xusch/regexp/analyzer.html是一个简单的正则表达式分析器(等等)。 Since its not linkable , here is just a screenshot and the link : 由于它不可链接 ,这里 只是 一个截图和链接

在此处输入图像描述

I believe you are looking for is the split() function:我相信您正在寻找的是split() function:

What you mentioned about turning words into an array is typically referred to as a split , so if you wanted to get " otherword " from " facebox otherword ", you would do the following:您提到的将单词转换为数组通常称为split ,因此如果您想从“ facebox otherword ”中获取“ otherword ”,您可以执行以下操作:

var test = "facebox otherword"
var word = test.split(' ')[1];

Working Demo工作演示

Your regex matches:您的正则表达式匹配:

facebox.abc脸盒.abc
facebox[.abc脸盒[.abc
facebox[.abc]脸盒[.abc]
facebox.abc] facebox.abc]

with abc being at least one "word character" (letters, digits, and underscores). abc至少是一个“单词字符”(字母、数字和下划线)。

Note \.注意\. . . That means that it is a dot, rather than any character.这意味着它是一个点,而不是任何字符。 This regex cathches facebox.otherword这个正则表达式捕获facebox.otherword

This matches text like:这匹配如下文本:

facebox[.blabla]
facebox.blabla

The regex you have there won't match "facebook otherword", because of the \.由于\. . . It's looking for a literal ".", so it would match the following: "facebook.otherword", "facebook[.otherword", "facebook.otherword]", or "facebook[.otherword]".它正在寻找文字“.”,因此它将匹配以下内容:“facebook.otherword”、“facebook[.otherword”、“facebook.otherword]”或“facebook[.otherword]”。

Checks that the string contained in the rel property of this检查包含在thisrel属性中的字符串

  1. contains the text 'facebox'包含文本'facebox'
  2. followed by 0 or 1 open-square-brackets后跟 0 或 1 个开方括号
  3. followed by a dot后跟一个点
  4. followed by 1 or more word characters后跟 1 个或多个单词字符
  5. followed by 0 or 1 close-square-brackets后跟 0 或 1 个右方括号

If matched, the 1 or more word characters will be contained in a sub-match.如果匹配,则 1 个或多个单词字符将包含在子匹配中。

/facebox\[?\.(\w+)\]?/

  • matches facebox literal匹配facebox字面量
  • then optionally match [然后可选地匹配[
  • then match .然后匹配.
  • then match any word然后匹配任何单词
  • then optionally match ]然后可选地匹配]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM