Nik*_*iko 129 javascript regex expression readability jslint
我有一个很长的正则表达式,我想在我的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,}))$/;
Run Code Online (Sandbox Code Playgroud)
Koo*_*Inc 108
您可以将其转换为字符串并通过调用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(''));
Run Code Online (Sandbox Code Playgroud)
笔记:
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);
})();Run Code Online (Sandbox Code Playgroud)
kor*_*run 104
扩展@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
);
Run Code Online (Sandbox Code Playgroud)
或者如果您想避免重复该.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(''));
Run Code Online (Sandbox Code Playgroud)
在ES6中,map函数可以简化为:
.map(r => r.source)
Ric*_*lli 24
使用字符串new RegExp很难,因为你必须转义所有的反斜杠.您可以编写较小的正则表达式并将它们连接起来.
让我们分开这个正则表达式
/^foo(.*)\bar$/
Run Code Online (Sandbox Code Playgroud)
我们将使用一个函数来使事情更美好
function multilineRegExp(regs, options) {
return new RegExp(regs.map(
function(reg){ return reg.source; }
).join(''), options);
}
Run Code Online (Sandbox Code Playgroud)
现在让我们摇滚吧
var r = multilineRegExp([
/^foo/, // we can add comments too
/(.*)/,
/\bar$/
]);
Run Code Online (Sandbox Code Playgroud)
由于它有成本,尝试只构建一次真正的正则表达式,然后使用它.
感谢模板文字的奇妙世界,您现在可以在 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])
))
);
Run Code Online (Sandbox Code Playgroud)
使用它,您现在可以像这样编写正则表达式:
let re = regex`I'm a special regex{3} //with a comment!`;
Run Code Online (Sandbox Code Playgroud)
输出
/I'm a special regex{3}/
Run Code Online (Sandbox Code Playgroud)
'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]
Run Code Online (Sandbox Code Playgroud)
输出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*[,}{]
).*)
$`
Run Code Online (Sandbox Code Playgroud)
它输出 /^\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]);
Run Code Online (Sandbox Code Playgroud)
成功输出
{why
, hello
, there
, "you huge \""
, 17
,
{big
,smelly
}
}
Run Code Online (Sandbox Code Playgroud)
请注意成功捕获带引号的字符串。
我在 Chrome 和 Firefox 上测试过,效果很好!
如果好奇,你可以检出我在做什么,以及它的示范。
虽然它只适用于 Chrome,因为 Firefox 不支持反向引用或命名组。所以请注意这个答案中给出的例子实际上是一个绝育版本,可能很容易被欺骗接受无效的字符串。
小智 7
这里有很好的答案,但为了完整起见,有人应该提到 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]/gRun Code Online (Sandbox Code Playgroud)
上面的正则表达式缺少一些无法正常工作的黑色斜线。所以,我编辑了正则表达式。请考虑这个正则表达式,它可以 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(''));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40044 次 |
| 最近记录: |