When I use javascript write regex.
当我使用javascript写regex时。
var regex = new RegExp("^[ ]{"+p1.length+"}\* ([^\n]*)", "gm")
btw, i have alert(p1.length)
顺便说一句,我已经提醒(p1.length)
it's value 0
.
它的值0。
i got error:
我得到了错误:
SyntaxError: Invalid regular expression: nothing to repeat
SyntaxError:无效的正则表达式:不需要重复
why?
为什么?
this is same error
这是同样的错误
var regex = new RegExp("^ {"+p1.length+"}\* ([^\n]*)", "gm")
but this is right:
但这是正确的:
var r="^ {"+p1.length+"}[0-9]+\. ([^\n]*)"
var regex = new RegExp(r, "gm")
my question is why???
我的问题是为什么? ? ?
var regex = new RegExp("^ {"+p1.length+"}[*] ([^\n]*)", "gm") //this is ok
var regex = new RegExp("^ {"+p1.length+"}\\* ([^\n]*)", "gm") //and this is ok too.
so when we use string build a regex, we need double\\ it is so trick.
所以当我们使用字符串构建一个regex时,我们需要双\\它是如此的巧妙。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
{n} Matches exactly n occurrences of the preceding expression. N must be a positive integer. For example, /a{2}/ doesn't match the 'a' in "candy," but it does match all of the a's in "caandy," and the first two a's in "caaandy."
{n}精确匹配前一个表达式的n次出现。N必须是一个正整数。例如,{2}/与“candy”中的“a”不匹配,但它确实与“caandy”中的“a”和“caaandy”中的前两个“a”匹配。
so my use {0} is right, no problem. the problem is we need \\ in string to build regex, just like @Andreas say, thanks.
所以我用{0}是对的,没问题。问题是我们需要用\ in string来构建regex,就像@Andreas所说的,谢谢。
1 个解决方案
#1
3
First things first, you didn't escape your regexes. You should probably do that...
首先,你没有逃脱你的雷管。你应该这样做……
Your 2 erroneous regexes have 2 separate errors.
你的两个错误regex有两个单独的错误。
Your first one is trying to quantify [ ]{0}
with "0 or more times".
第一个是尝试用“0或更多次”量化{0}。
[ ]{0}
repeated 100 or 1000 or however many times is still an empty string. This means that the regex will never stop matching if this were valid, because it would not have proceeded to match any more characters after [ ]{0}*
.
[]{0}重复100或1000或多少次仍然是一个空字符串。这意味着如果匹配有效,regex将永远不会停止匹配,因为在[]{0}*之后,regex将不会继续匹配任何字符。
Your second regex is trying to repeat ^
0 times.
你的第二个regex试图重复^ 0次。
^
is not a character, it is just a zero-width position - the start of the string. The same goes with other "positions" like \b
. There can't be 2 "starts" of a string next to each other.
^不是一个字符,它只是一个任意的位置——字符串的开始。同样的道理也适用于像\b这样的“位置”。不可能有两个“开始”的字符串相邻。
I will now explain why the two regexes in your edit works.
现在我将解释为什么在您的编辑工作中有两个regex。
[*]
is a character class. In a character class, *
loses its meaning of "zero or more times" and becomes literal. So you are just matching the character *
after no spaces.
是一个角色类。在字符类中,*失去了“零或多次”的含义,变成了文字。所以你只是在没有空格后匹配字符*。
\\*
unescaped is just \*
. Here the *
loses its meaning again since it is after a backslash.
\* unescape就是\*。在这里,*再次失去意义,因为它是在反斜杠之后。
#1
3
First things first, you didn't escape your regexes. You should probably do that...
首先,你没有逃脱你的雷管。你应该这样做……
Your 2 erroneous regexes have 2 separate errors.
你的两个错误regex有两个单独的错误。
Your first one is trying to quantify [ ]{0}
with "0 or more times".
第一个是尝试用“0或更多次”量化{0}。
[ ]{0}
repeated 100 or 1000 or however many times is still an empty string. This means that the regex will never stop matching if this were valid, because it would not have proceeded to match any more characters after [ ]{0}*
.
[]{0}重复100或1000或多少次仍然是一个空字符串。这意味着如果匹配有效,regex将永远不会停止匹配,因为在[]{0}*之后,regex将不会继续匹配任何字符。
Your second regex is trying to repeat ^
0 times.
你的第二个regex试图重复^ 0次。
^
is not a character, it is just a zero-width position - the start of the string. The same goes with other "positions" like \b
. There can't be 2 "starts" of a string next to each other.
^不是一个字符,它只是一个任意的位置——字符串的开始。同样的道理也适用于像\b这样的“位置”。不可能有两个“开始”的字符串相邻。
I will now explain why the two regexes in your edit works.
现在我将解释为什么在您的编辑工作中有两个regex。
[*]
is a character class. In a character class, *
loses its meaning of "zero or more times" and becomes literal. So you are just matching the character *
after no spaces.
是一个角色类。在字符类中,*失去了“零或多次”的含义,变成了文字。所以你只是在没有空格后匹配字符*。
\\*
unescaped is just \*
. Here the *
loses its meaning again since it is after a backslash.
\* unescape就是\*。在这里,*再次失去意义,因为它是在反斜杠之后。