I am trying to create a regular expression that will not match words in quotes but will match words without quotes e.g
-"Welcome" - false
-Welcome - true
I am trying to create a regular expression that will not match words in quotes but will match words without quotes e.g
-"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