简体   繁体   English

如何将此正则表达式拆分为更具可读性?

[英]How can I split this regex up to make more readable?

and still keep it in the object literal: 并将其保留在对象文字中:

url:       /:\/{0,3}(www\.)?([0-9.\-A-Za-z]{1,253})([\x00-\x7F]{1,2000})$/,

In addition how can I simplify it. 此外,我如何简化它。

It is just a mess in the current state. 这只是当前状态的混乱。 I'm not worried about accuracy right now. 我现在不担心准确性。

Here is my try from Crockford's book: 这是我在Crockford的书中的尝试:

makeRegex: function () {
    var parse_url = /^(?:([A-Za-z]+):)?(\/{0,3})
                    ([0-9.\-A-Za-z]+)
                    (?::(\d+))
                    ?(?:\/([^?#]*))
                    ?(?:\?([^#]*))
                    ?(?:#(.*))?$/; 
},

Regular expressions are notoriously unreadable. 众所周知,正则表达式是不可读的。 They don't like extra spaces and they don't have comments. 他们不喜欢额外的空格,也没有评论。 Your only possible solution is to construct a string and then turn that into a regular expression. 您唯一可行的解​​决方案是构造一个字符串,然后将其转换为正则表达式。

Here are the steps I went trough 这是我走过的台阶

Target Regular Expression 目标正则表达式

var regex=/:\/{0,3}(www\.)?([0-9.\-A-Za-z]{1,253})([\x00-\x7F]{1,2000})$/;

Use RegExp to construct the expression from a string. 使用RegExp从字符串构造表达式。

var parse_url = RegExp(':/{0,3}(www\\.)?([0-9.\\-A-Za-z]{1,253})([\\x00-\\x7F]{1,2000})$');

Remember: 记得:

  • the / delimiters at the beginning and the end of the expression are not there — they're only in a RegEx literal 表达式开头和结尾的/分隔符不存在 - 它们只存在于RegEx文字中
  • the \\ characters in the string are doubled, because the string has its own interpretation of them 字符串中的\\字符加倍,因为字符串有自己的解释

Break the string up by adding '+' as strategic points: 通过添加'+'作为战略要点来打破字符串:

var parse_url = RegExp(':/{0,3}(www\\.)?'+'([0-9.\\-A-Za-z]{1,253})'+'([\\x00-\\x7F]{1,2000})$');

var parse_url = RegExp(':/{0,3}(www\\.)?'+
    '([0-9.\\-A-Za-z]{1,253})'+
    '([\\x00-\\x7F]{1,2000})$');

It's not a very good solution, but that's all you can do with a regular expression. 这不是一个非常好的解决方案,但是你可以用正则表达式做所有事情。

Modern JavaScript does support multi-line strings in the form of the template literals, but that probably won't help much here. 现代JavaScript确实支持模板文字形式的多行字符串,但这可能在这里没有多大帮助。

I suggest breaking a regular expression into parts and assigning each part to a well-named variable, with a comment if necessary. 我建议将正则表达式分成几部分,并将每个部分分配给一个命名良好的变量,必要时带注释。 An example, which is meant to demonstrate the principle rather than correctly validate URLs, since a URL-matching regex is hard to write ( https://mathiasbynens.be/demo/url-regex ): 一个示例,旨在演示原理而不是正确验证URL,因为URL匹配正则表达式很难编写( https://mathiasbynens.be/demo/url-regex ):

var protocol = '(?:https?|ftp)'; // Protocol can be "http", "https" or "ftp"
var domain = '([A-Za-z0-9\.]+)'; // Alphanumeric characters separated by periods
var path = '(?:[A-Za-z0-9\.\/]+)'; // Alphanumeric characters, . or /
var regexp = Regexp(protocol + '://' + domain + '/' + path);

Now you have the regular expression broken into smaller, more easily understood mini-expressions, and the overall expression is a lot easier to read. 现在,您将正则表达式分解为更小,更容易理解的迷你表达式,并且整体表达式更容易阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使承诺更加易读? - How can I make use of promises more readable? 如何使此发布/子代码更具可读性? - How can I make this pub/sub code more readable? 如何干掉我的jQuery代码并使其更具可读性/可维护性? - How to DRY up my jQuery code and make it more readable/maintanable? 如何使 React Components Dev 工具更易读/更有用? - How can I make the React Components Dev tool readable / more useful? 如何在我的react组件中重构此ASYNC调用以使其更具可读性? - How can I refactor this ASYNC call in my react component to make it more readable? 如何在不损失性能的情况下缩小或至少使我的代码更具可读性? - How can I minify or at least make my code more readable without losing on performance? 如何使 javascript map、减少语法更短、更易读? - How can I make the javascript map, reduce syntax shorter and more readable? 你怎么能重构并使反应变得不那么笨重和可读性? - How can you refactor and make react less bulky and more readable? 如何为一个或多个用空格分隔的数字制作正则表达式? - How can I make a regex for one or more space delimited numbers? 我怎样才能缩短这个 JS 代码以使其更具可读性? - How can I possible shorten this JS code to be more readable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM