简体   繁体   English

转义双引号正则表达式JavaScript

[英]Escape double quotes regular expression JavaScript

I have a simple question. 我有一个简单的问题。 How can I match any string that is not enclosed in double quotes or/and < and > characters sequences? 如何匹配未用双引号或/和<和>字符序列括起来的任何字符串?

Example. 例。 I got this link. 我得到了这个链接。 And I don't want to match it if it is enclosed in double quotes. 如果它用双引号引起来,我也不想匹配它。

((https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])

This doesn't work, by putting [^"] at the beggining and at the end: 将[^“]放在开头和结尾处是不起作用的:

([^"](https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|][^"])

Because links that are not enclosed in double quotes will not be matched unless they start with something else than a doule quote. 因为没有用双引号引起来的链接将不匹配,除非它们以双引号引起来。

I wouldn't try to do it in a single RegEx since, as you've noticed, it's probably not going to work. 我不会尝试在单个RegEx中执行此操作,因为您已经注意到,它可能无法正常工作。

Assuming you're trying to do something like auto-linking, including optional quotes in your search, then discarding them after, would work: 假设您要尝试进行自动链接之类的操作,包括在搜索中包含可选的引号,然后再将其丢弃,则可以:

var re = /"?(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|]"?/gi;
var quoted = /^".+"$/;

var txt = 'A real link: http://www.example.com and a quoted link: "http://www.example.com".';

txt = txt.replace(re, 
            function(str) {
              if (str.match(quoted))
                return str;
              else
                return '<a href="' + str + '">' + str + '</a>';
            }
            );

$('#mydiv').html(txt);

A working example: http://codepen.io/paulroub/pen/nlExh 一个有效的示例: http : //codepen.io/paulroub/pen/nlExh

As Esailija suggested, surrounding your regex with ^ and $ will work. 正如Esailija所建议的,用^和$包围正则表达式将起作用。 Try this in your console. 在您的控制台中尝试。 Here's the regex matching a bare URL: 这是匹配裸URL的正则表达式:

> ' http://www.something.com '.match(/^((https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|])$/i) >' http ://www.something.com'.match(/ ^((https?| ftp | file):// [-A-Z0-9 +&@#/%?=〜_ |!:, 。;] * [-A-Z0-9 +&@#/%=〜_ |])$ / i)

[" http://www.something.com ", " http://www.something.com ", "http"] [“ http://www.something.com ”,“ http://www.something.com ”,“ http”]

and here it is not matching a URL embedded in double quotes or angle brackets: 并且此处与嵌入在双引号或尖括号中的URL不匹配:

>'" http://www.something.com "'.match(/^((https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|])$/i) >'“ http://www.something.com ”'.match(/ ^((https?| ftp | file)://://-Z0-9 +&@#/%?=〜_ |! :,。;] * [-A-Z0-9 +&@#/%=〜_ |])$ / i)

null 空值

>'> http://www.something.com <'.match(/^((https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|])$/i) >'> http://www.something.com <'.match(/ ^((https?| ftp | file)://://-Z0-9 +&@#/%?=〜_ |! :,。;] * [-A-Z0-9 +&@#/%=〜_ |])$ / i)

null 空值

Don't pick this answer as I'm just reiterating what Esailija suggested. 不要选择这个答案,因为我只是在重申Esailija的建议。

It's possible because this works: 可能是因为这样做有效:

<script>

var str='https://test.com/';
var patt=/[^"].*[^"]/g;

var result=patt.test(str);
document.write("Returned value: " +  result); 

str='"http://test.com/"';
result=patt.test(str);
document.write("<br>Returned value: " +  result);

</script>
</body>
</html>

Maybe it's your regexp for the link that is wrong 也许是您的正则表达式链接错误

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM