我有一个很长的正则表达式,我希望在JavaScript代码中将其分成多行,以根据JSLint规则将每行长度保持80个字符。我认为这对阅读更好。这是模式样本:
var pattern = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
您可以将其转换为字符串并通过调用创建表达式new RegExp()
:
var myRE = new RegExp (['^(([^<>()[\]\\.,;:\\s@\"]+(\\.[^<>(),[\]\\.,;:\\s@\"]+)*)',
'|(\\".+\\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.',
'[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\\.)+',
'[a-zA-Z]{2,}))$'].join(''));
笔记:
- 将表达式文字转换为字符串时,您需要对所有反斜杠进行转义,因为在评估字符串文字时会消耗反斜杠。(有关更多详细信息,请参见Kayo的评论。)
-
RegExp
接受修饰符作为第二个参数/regex/g
=>new RegExp('regex', 'g')
[添加ES20xx(标记模板)]
在ES20xx中,您可以使用标记的模板。请参阅摘要。
注意:
- 这里缺点是,你不能在正则表达式字符串使用纯空格(经常使用
\s
,\s+
,\s{1,x}
,\t
,\n
等)。
(() => {
const createRegExp = (str, opts) =>
new RegExp(str.raw[0].replace(/\s/gm, ""), opts || "");
const yourRE = createRegExp`
^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|
(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|
(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$`;
console.log(yourRE);
const anotherLongRE = createRegExp`
(\byyyy\b)|(\bm\b)|(\bd\b)|(\bh\b)|(\bmi\b)|(\bs\b)|(\bms\b)|
(\bwd\b)|(\bmm\b)|(\bdd\b)|(\bhh\b)|(\bMI\b)|(\bS\b)|(\bMS\b)|
(\bM\b)|(\bMM\b)|(\bdow\b)|(\bDOW\b)
${"gi"}`;
console.log(anotherLongRE);
})();
扩展@KooiInc答案,您可以避免使用对象的source
属性手动转义每个特殊字符RegExp
。
例:
var urlRegex= new RegExp(''
+ /(?:(?:(https?|ftp):)?\/\/)/.source // protocol
+ /(?:([^:\n\r]+):([^@\n\r]+)@)?/.source // user:pass
+ /(?:(?:www\.)?([^\/\n\r]+))/.source // domain
+ /(\/[^?\n\r]+)?/.source // request
+ /(\?[^#\n\r]*)?/.source // query
+ /(#?[^\n\r]*)?/.source // anchor
);
或者,如果您想避免重复该.source
属性,则可以使用以下Array.map()
功能:
var urlRegex= new RegExp([
/(?:(?:(https?|ftp):)?\/\/)/ // protocol
,/(?:([^:\n\r]+):([^@\n\r]+)@)?/ // user:pass
,/(?:(?:www\.)?([^\/\n\r]+))/ // domain
,/(\/[^?\n\r]+)?/ // request
,/(\?[^#\n\r]*)?/ // query
,/(#?[^\n\r]*)?/ // anchor
].map(function(r) {return r.source}).join(''));
在ES6中,地图功能可以简化为:
.map(r => r.source)
在中使用字符串new RegExp
很麻烦,因为您必须转义所有反斜杠。您可以编写较小的正则表达式并将其连接起来。
让我们拆分此正则表达式
/^foo(.*)\bar$/
稍后我们将使用一个函数使事物更美丽
function multilineRegExp(regs, options) {
return new RegExp(regs.map(
function(reg){ return reg.source; }
).join(''), options);
}
现在让我们摇滚
var r = multilineRegExp([
/^foo/, // we can add comments too
/(.*)/,
/\bar$/
]);
由于这是有成本的,因此请尝试只构建一次真正的正则表达式,然后再使用它。
由于模板文字的奇妙世界,您现在可以在ES6中编写大型的,多行的,注释良好的甚至是语义嵌套的正则表达式。
//build regexes without worrying about
// - double-backslashing
// - adding whitespace for readability
// - adding in comments
let clean = (piece) => (piece
.replace(/((^|\n)(?:[^\/\\]|\/[^*\/]|\\.)*?)\s*\/\*(?:[^*]|\*[^\/])*(\*\/|)/g, '$1')
.replace(/((^|\n)(?:[^\/\\]|\/[^\/]|\\.)*?)\s*\/\/[^\n]*/g, '$1')
.replace(/\n\s*/g, '')
);
window.regex = ({raw}, ...interpolations) => (
new RegExp(interpolations.reduce(
(regex, insert, index) => (regex + insert + clean(raw[index + 1])),
clean(raw[0])
))
);
现在,您可以使用以下代码编写正则表达式:
let re = regex`I'm a special regex{3} //with a comment!`;
产出
/I'm a special regex{3}/
'123hello'
.match(regex`
//so this is a regex
//here I am matching some numbers
(\d+)
//Oh! See how I didn't need to double backslash that \d?
([a-z]{1,3}) /*note to self, this is group #2*/
`)
[2]
输出hel
,整齐!
“如果我需要实际搜索换行符怎么办?”,那就\n
傻了!
在我的Firefox和Chrome上工作。
好的,“稍微复杂一点?”
当然,这是我正在处理的对象分解JS解析器的一部分:
regex`^\s*
(
//closing the object
(\})|
//starting from open or comma you can...
(?:[,{]\s*)(?:
//have a rest operator
(\.\.\.)
|
//have a property key
(
//a non-negative integer
\b\d+\b
|
//any unencapsulated string of the following
\b[A-Za-z$_][\w$]*\b
|
//a quoted string
//this is #5!
("|')(?:
//that contains any non-escape, non-quote character
(?!\5|\\).
|
//or any escape sequence
(?:\\.)
//finished by the quote
)*\5
)
//after a property key, we can go inside
\s*(:|)
|
\s*(?={)
)
)
((?:
//after closing we expect either
// - the parent's comma/close,
// - or the end of the string
\s*(?:[,}\]=]|$)
|
//after the rest operator we expect the close
\s*\}
|
//after diving into a key we expect that object to open
\s*[{[:]
|
//otherwise we saw only a key, we now expect a comma or close
\s*[,}{]
).*)
$`
它输出 /^\s*((\})|(?:[,{]\s*)(?:(\.\.\.)|(\b\d+\b|\b[A-Za-z$_][\w$]*\b|("|')(?:(?!\5|\\).|(?:\\.))*\5)\s*(:|)|\s*(?={)))((?:\s*(?:[,}\]=]|$)|\s*\}|\s*[{[:]|\s*[,}{]).*)$/
并运行一个小演示?
let input = '{why, hello, there, "you huge \\"", 17, {big,smelly}}';
for (
let parsed;
parsed = input.match(r);
input = parsed[parsed.length - 1]
) console.log(parsed[1]);
成功输出
{why
, hello
, there
, "you huge \""
, 17
,
{big
,smelly
}
}
请注意成功捕获带引号的字符串。
我在Chrome和Firefox上测试过,效果不错!
如果好奇的话,您可以查看我在做什么,以及它的示范。
尽管它仅适用于Chrome,但由于Firefox不支持反向引用或命名组。因此请注意,此答案中给出的示例实际上是一个绝望的版本,可能很容易被欺骗来接受无效的字符串。
这里有很好的答案,但是为了完整起见,应该使用原型链提及Javascript继承的核心功能。这样的事情说明了这个想法:
RegExp.prototype.append = function(re) {
return new RegExp(this.source + re.source, this.flags);
};
let regex = /[a-z]/g
.append(/[A-Z]/)
.append(/[0-9]/);
console.log(regex); //=> /[a-z][A-Z][0-9]/g
上面的正则表达式缺少一些不能正常工作的黑色斜杠。因此,我编辑了正则表达式。请考虑使用此正则表达式,该正则表达式对电子邮件验证有效率为99.99%。
let EMAIL_REGEXP =
new RegExp (['^(([^<>()[\\]\\\.,;:\\s@\"]+(\\.[^<>()\\[\\]\\\.,;:\\s@\"]+)*)',
'|(".+"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.',
'[0-9]{1,3}\])|(([a-zA-Z\\-0-9]+\\.)+',
'[a-zA-Z]{2,}))$'].join(''));
为了避免Array join
,您还可以使用以下语法:
var pattern = new RegExp('^(([^<>()[\]\\.,;:\s@\"]+' +
'(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@' +
'((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|' +
'(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$');
就个人而言,我会选择一个不太复杂的正则表达式:
/\S+@\S+\.\S+/
当然,它的准确性不如您当前的模式,但是您要达到什么目的?您是要抓住用户可能输入的意外错误,还是担心用户可能要输入无效的地址?如果是第一次,我会选择一个更简单的模式。如果是后者,则最好通过回复发送到该地址的电子邮件进行验证。
但是,如果要使用当前模式,则可以通过较小的子模式构建它,从而使(IMO)更加易于阅读(并维护!),如下所示:
var box1 = "([^<>()[\]\\\\.,;:\s@\"]+(\\.[^<>()[\\]\\\\.,;:\s@\"]+)*)";
var box2 = "(\".+\")";
var host1 = "(\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])";
var host2 = "(([a-zA-Z\-0-9]+\\.)+[a-zA-Z]{2,})";
var regex = new RegExp("^(" + box1 + "|" + box2 + ")@(" + host1 + "|" + host2 + ")$");
您可以简单地使用字符串操作。
var pattenString = "^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|"+
"(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|"+
"(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$";
var patten = new RegExp(pattenString);
我试图通过封装所有内容并实现对拆分捕获组和字符集的支持来改善korun的答案-使此方法更加通用。
要使用此代码段,您需要调用可变参数函数,combineRegex
其参数是您需要组合的正则表达式对象。它的实现可以在底部找到。
捕获组不能以这种方式直接拆分,因为这样会使某些部分仅带有一个括号。您的浏览器将因异常而失败。
相反,我只是在数组中传递捕获组的内容。combineRegex
遇到数组时,括号会自动添加。
此外,量词还需要遵循一些规则。如果由于某种原因正则表达式需要在量词前进行拆分,则需要添加一对括号。这些将被自动删除。关键是空的捕获组几乎没有用,因此量词有一定的参考意义。可以将相同的方法用于非捕获组(/(?:abc)/
变为[/()?:abc/]
)之类的事情。
最好用一个简单的例子来解释一下:
var regex = /abcd(efghi)+jkl/;
会成为:
var regex = combineRegex(
/ab/,
/cd/,
[
/ef/,
/ghi/
],
/()+jkl/ // Note the added '()' in front of '+'
);
如果必须拆分字符集,则可以使用对象({"":[regex1, regex2, ...]}
)而不是数组([regex1, regex2, ...]
)。密钥的内容可以是任何内容,只要对象仅包含一个密钥即可。请注意,如果第一个字符可以解释为量词,则不必()
使用它]
作为虚拟开头。即/[+?]/
成为{"":[/]+?/]}
这是代码段和更完整的示例:
function combineRegexStr(dummy, ...regex)
{
return regex.map(r => {
if(Array.isArray(r))
return "("+combineRegexStr(dummy, ...r).replace(dummy, "")+")";
else if(Object.getPrototypeOf(r) === Object.getPrototypeOf({}))
return "["+combineRegexStr(/^\]/, ...(Object.entries(r)[0][1]))+"]";
else
return r.source.replace(dummy, "");
}).join("");
}
function combineRegex(...regex)
{
return new RegExp(combineRegexStr(/^\(\)/, ...regex));
}
//Usage:
//Original:
console.log(/abcd(?:ef[+A-Z0-9]gh)+$/.source);
//Same as:
console.log(
combineRegex(
/ab/,
/cd/,
[
/()?:ef/,
{"": [/]+A-Z/, /0-9/]},
/gh/
],
/()+$/
).source
);
@Hashbrown的好答案使我走上了正确的轨道。这是我的版本,也受到此博客的启发。
function regexp(...args) {
function cleanup(string) {
// remove whitespace, single and multi-line comments
return string.replace(/\s+|\/\/.*|\/\*[\s\S]*?\*\//g, '');
}
function escape(string) {
// escape regular expression
return string.replace(/[-.*+?^${}()|[\]\\]/g, '\\$&');
}
function create(flags, strings, ...values) {
let pattern = '';
for (let i = 0; i < values.length; ++i) {
pattern += cleanup(strings.raw[i]); // strings are cleaned up
pattern += escape(values[i]); // values are escaped
}
pattern += cleanup(strings.raw[values.length]);
return RegExp(pattern, flags);
}
if (Array.isArray(args[0])) {
// used as a template tag (no flags)
return create('', ...args);
}
// used as a function (with flags)
return create.bind(void 0, args[0]);
}
像这样使用它:
regexp('i')`
//so this is a regex
//here I am matching some numbers
(\d+)
//Oh! See how I didn't need to double backslash that \d?
([a-z]{1,3}) /*note to self, this is group #2*/
`
要创建此RegExp
对象:
/(\d+)([a-z]{1,3})/i
文章标签:expression , javascript , jslint , readability , regex
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
评论已关闭!