简体   繁体   English

是否有一个正则表达式会忽略被引号包围的单词

[英]Is there a regular expression that will ignore words that are surronded by quotes

I am trying to create a regular expression that will not match words in quotes but will match words without quotes eg我正在尝试创建一个正则表达式,它不会匹配引号中的单词,但会匹配没有引号的单词,例如

-"Welcome" - false -“欢迎” - 假
-Welcome - true -欢迎 - 真的

You could match only those words that are followed by an even number of double quotes, which means they themselves are not within quotes (assuming your quotes are always paired):您只能匹配那些后跟偶数双引号的单词,这意味着它们本身不在引号内(假设您的引号始终成对):

\w+(?=[^"]*("[^"]*"[^"]*)*$)

 function getWord(txt) { txt = txt.match(/\\w+(?=[^"]*("[^"]*"[^"]*)*$)/); return txt && txt[0]; } var input = document.querySelector('input'); var output = document.querySelector('span'); input.oninput = function () { output.textContent = getWord(input.value); } input.oninput();
 Word: <input value='"two" words'><br> First non-quoted: <span></span>

(?=([^"]*"[^"]*")*[^"]*$) will exclude all double quotes in the string and all text within those double quotes. (?=([^"]*"[^"]*")*[^"]*$)将排除字符串中的所有双引号以及这些双引号内的所有文本。
This will also remove all newline characters if you have any如果您有任何换行符,这也将删除所有换行符
Here is my source 是我的来源

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

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