ple*_*and 12 javascript regex comments
我正试图在JavaScript中评论正则表达式.
关于如何使用正则表达式从代码中删除注释似乎有很多资源,但实际上并不是如何在JavaScript中注释正则表达式以便更容易理解.
任何帮助是极大的赞赏!
Exp*_*lls 14
遗憾的是,JavaScript并没有像其他语言那样对正则表达式文字进行冗长的模式.不过,你可能会发现这很有趣.
代替任何外部库,最好的办法就是使用普通字符串并注释:
var r = new RegExp(
'(' + //start capture
'[0-9]+' + // match digit
')' //end capture
);
r.test('9'); //true
Run Code Online (Sandbox Code Playgroud)
虽然 Javascript 本身不支持多行和带注释的正则表达式,但构建完成相同功能的东西很容易 - 使用一个函数接收(多行,带注释的)字符串并从该字符串返回正则表达式, 没有注释和换行符。
以下代码段模仿其他风格x
(“扩展”)标志的行为,它忽略模式中的所有空白字符以及注释,用 表示#
:
function makeExtendedRegExp(inputPatternStr, flags) {
// Remove everything between the first unescaped `#` and the end of a line
// and then remove all unescaped whitespace
const cleanedPatternStr = inputPatternStr
.replace(/(^|[^\\])#.*/g, '$1')
.replace(/(^|[^\\])\s+/g, '$1');
return new RegExp(cleanedPatternStr, flags);
}
// The following switches the first word with the second word:
const input = 'foo bar baz';
const pattern = makeExtendedRegExp(String.raw`
^ # match the beginning of the line
(\w+) # 1st capture group: match one or more word characters
\s # match a whitespace character
(\w+) # 2nd capture group: match one or more word characters
`);
console.log(input.replace(pattern, '$2 $1'));
Run Code Online (Sandbox Code Playgroud)
通常,要在 Javascript 字符串中表示反斜杠,必须对每个文字反斜杠进行双重转义,例如str = 'abc\\def'
. 但是正则表达式经常使用很多反斜杠,而双重转义会使模式的可读性大大降低,所以在编写带有很多反斜杠的 Javascript 字符串时,最好使用String.raw
模板文字,它允许单个类型的反斜杠实际表示一个字面反斜杠,无需额外转义。
就像使用标准x
修饰符一样,要匹配#
字符串中的实际值,只需先对其进行转义,例如
foo\#bar # comments go here
Run Code Online (Sandbox Code Playgroud)
foo\#bar # comments go here
Run Code Online (Sandbox Code Playgroud)
请注意,要匹配文字空格字符(而不仅仅是任何空白字符),x
在任何环境(包括上述环境)中使用标志时,您必须使用\
第一个对空格进行转义,例如:
^(\S+)\ (\S+) # capture the first two words
Run Code Online (Sandbox Code Playgroud)
如果您想经常匹配空格字符,这可能会变得有点乏味并且使模式更难阅读,类似于双重转义反斜杠不是很可取。允许未转义空格字符的一种可能(非标准)修改是仅去除行首和行尾的空格以及#
注释前的空格:
// this function is exactly the same as the one in the first snippet
function makeExtendedRegExp(inputPatternStr, flags) {
// Remove everything between the first unescaped `#` and the end of a line
// and then remove all unescaped whitespace
const cleanedPatternStr = inputPatternStr
.replace(/(^|[^\\])#.*/g, '$1')
.replace(/(^|[^\\])\s+/g, '$1');
return new RegExp(cleanedPatternStr, flags);
}
// The following switches the first word with the second word:
const input = 'foo#bar baz';
const pattern = makeExtendedRegExp(String.raw`
^ # match the beginning of the line
(\w+) # 1st capture group: match one or more word characters
\# # match a hash character
(\w+) # 2nd capture group: match one or more word characters
`);
console.log(input.replace(pattern, '$2 $1'));
Run Code Online (Sandbox Code Playgroud)
在其他几种语言(特别是 Perl)中,有一个特殊的x
标志。设置后,正则表达式会忽略其中的任何空格和注释。遗憾的是,javascript 正则表达式不支持该x
标志。
由于缺乏语法,利用可读性的唯一方法是约定。我的方法是在棘手的正则表达式之前添加注释,包含它,就像您有 x 标志一样。例子:
/*
\+? #optional + sign
(\d*) #the integeric part
( #begin decimal portion
\.
\d+ #decimal part
)
*/
var re = /\+?(\d*)(\.\d+)/;
Run Code Online (Sandbox Code Playgroud)
对于更复杂的示例,您可以在此处和此处查看我使用该技术所做的工作。