[英]Javascript string split with regex
I am trying to split a string using a regular expression for links (urls). 我试图使用链接(URL)的正则表达式拆分字符串。
The regex in question is 正则表达式是
var regex = new RegExp('(?:^(?:(?:[a-z]+:)?//)(?:\S+(?::\S*)?@)?(?:localhost|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:[/?#]\S*)?$)')
If i do 如果我做
regex.test("https://google.com"); // returns true
but doing - 但是在做-
"Go to https://google.com".split(regex);
// return ["Go to https://google.com"]
Whereas i expect it to return 而我希望它能回来
["Go to ", "https://google.com"]
Any idea what's going on here? 知道这里发生了什么吗?
First of all, you're using a string literal to build your regex, which means that you have to escape your backslashes (since a backslash has a special meaning in strings, used for the line feed char \\n
for example): 首先,您使用字符串文字来构建您的正则表达式,这意味着您必须转义反斜杠(因为反斜杠在字符串中具有特殊含义,例如用于换行符char \\n
):
var regex = new RegExp('(?:^(?:(?:[a-z]+:)?//)(?:\\S+(?::\\S*)?@)?(?:localhost|(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))(?::\\d{2,5})?(?:[/?#]\\S*)?$)');
Another solution would be to use the regex literal, as JavaScript proposes one, but you would then have to escape the slashes: 另一种解决方案是使用正则表达式文字,如JavaScript所建议的那样,但是您必须转义斜线:
var regex = /(?:^(?:(?:[a-z]+:)?\/\/)(?:\S+(?::\S*)?@)?(?:localhost|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:[\/?#]\S*)?$)/;
Then, your regex will try to match against the entire input due to the ^
and $
anchors . 然后,由于^
和$
anchors ,您的正则表达式将尝试与整个输入匹配。 So if you remove them (or better, replace them with word boundaries \\b
), you'll be able to find URLs in a string for example: 因此,如果删除它们(或者最好用单词边界 \\b
替换它们),则可以在字符串中找到URL,例如:
var regex = /(?:\b(?:(?:[a-z]+:)?\/\/)(?:\S+(?::\S*)?@)?(?:localhost|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:[\/?#]\S*)?\b)/;
But, the main point is that you're misunderstanding the split
concept. 但是,要点是您误解了split
概念。 Given the string "hello world"
, if you split by space, you'll end up with ["hello", "world"]
: no more space anymore since it was the char that was used to split. 给定字符串"hello world"
,如果您按空格分割,则将以["hello", "world"]
结尾:由于用于分割的char是不再有空格。
That is, if you split by the URL regex, the output array won't contain the URLs anymore. 也就是说,如果按URL正则表达式拆分,则输出数组将不再包含URL。 It seems to me that a lookahead could suit your needs: 在我看来,提前行可以满足您的需求:
var regex = /(?=(?:\b(?:(?:[a-z]+:)?\/\/)(?:\S+(?::\S*)?@)?(?:localhost|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:[\/?#]\S*)?\b))/;
"Go to https://google.com".split(regex) // ["Go to ", "https://google.com"]
The regex explained: 正则表达式说明:
(?=(?:\b(?:(?:[a-z]+:)?//)(?:\S+(?::\S*)?@)?(?:localhost|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:[/?#]\S*)?\b))
By splitting a string with a positive lookahead (?=content_of_lookahead)
, you'll split by each interchar that is followed by the content of the lookahead. 通过以正前瞻(?=content_of_lookahead)
拆分字符串,您(?=content_of_lookahead)
每个interchar进行拆分,后跟先行内容。
Take a look at 8 Regular Expressions You Should Know . 看一下您应该知道的8个正则表达式 。
To match an url you can use following regex : 要匹配网址,您可以使用以下正则表达式:
var regex = "(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w# \.-]*)*\/?$";
"Go to https://google.com".split(regex);
// return ["https://google.com"]
Live example . 现场例子 。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.