[英]JavaScript RegEx end of string not working
I want to redirect certain facebook urls. 我想重定向某些Facebook网址。 So far I have a working chrome plugin , but some urls are not matched the right way.
到目前为止,我有一个工作正常的chrome插件 ,但是某些网址与正确的方式不匹配。
var patternURL = new RegExp("http(s)?://www\.facebook\.com(/|/@?ref=(tn_tnmn|logo)+)?$", "m");
Should match: 应该匹配:
https://www.facebook.com/?ref=tn_tnmn
https://www.facebook.com/?ref=tn_tnmn
https://www.facebook.com/?ref=logohttps://www.facebook.com/?ref=logo
https://www.facebook.com/https://www.facebook.com/
https://www.facebook.comhttps://www.facebook.com
But not: 但不是:
https://www.facebook.com/messages/
https://www.facebook.com/messages/
It seams in JavaScript the end of string ( $ ) is not recognized correctly. 它在JavaScript中接缝,无法正确识别字符串( $ )的末尾。 Thats what my jsfiddle demo shows so far.
到目前为止,这就是我的jsfiddle演示所显示的。 It should be True but is always false even with "m".
它应该为True,但即使带有“ m”也始终为false。
What do I do wrong? 我做错了什么?
The ?
?
is not escaped. 不能逃脱。 You need to use two backslashes to escape it.
您需要使用两个反斜杠将其转义。
patternURL = new RegExp("http(s)?://www\\.facebook\\.com(/|/\\\\?ref=(tn_tnmn|logo)+)?$", "m");
http://jsfiddle.net/tRm7U/8/ http://jsfiddle.net/tRm7U/8/
This is because when you use the regexp constructor, you use a string, and the first backslash is "used" to escape in the string before the regexp object is constructed. 这是因为当您使用regexp构造函数时,您使用了一个字符串,并且在构造regexp对象之前,“第一个反斜杠”用于字符串中的转义。 You don't need it if you declare it like this:
如果这样声明,则不需要它:
patternURL = /http(s)?://www\\.facebook\\.com(/|/\\?ref=(tn_tnmn|logo)+)?$/m
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.