像在Perl中一样在JavaScript正则表达式中嵌入注释 [英] Embed comments within JavaScript regex like in Perl

查看:155
本文介绍了像在Perl中一样在JavaScript正则表达式中嵌入注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以在JavaScript正则表达式中嵌入注释,例如你可以在Perl中做什么

Is there any way to embed a comment in a JavaScript regex, like you can do in Perl? I'm guessing there is not, but my searching didn't find anything stating you can or can't.

推荐答案

你在哪里?不能在正则表达式中嵌入注释。

You can't embed a comment in a regex literal.

您可以在传递给RegExp构造函数的字符串构造中插入注释:

You may insert comments in a string construction that you pass to the RegExp constructor :

var r = new RegExp(
    "\\b"   + // word boundary
    "A="    + // A=
    "(\\d+)"+ // what is captured : some digits
    "\\b"     // word boundary again
, 'i');       // case insensitive

但是一个正则表达式非常方便 \ )我宁愿将正则表达式与注释分开:只是在你的正则表达式之前,而不是里面加一些注释。

But a regex literal is so much more convenient (notice how I had to escape the \) I'd rather separate the regex from the comments : just put some comments before your regex, not inside.

这篇关于像在Perl中一样在JavaScript正则表达式中嵌入注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆