I am able to get all the .text" but not sure how to exclude the ones with "cat" word.
You can use the following regex:
/^(?!cat).*\.\s*text/m
If you want the match to not take letter case into account, you can add the case-insensitive i flag at the end (e.g., mi).
Explanation:
^ // assert position at start of a line (with the multiline 'm' flag)
(?!cat) // negative lookahead to prevent matching if line starts with cat
.* // match any number of characters excluding linebreak characters
\. // match "." literally
\s* // match any number of whitespace characters
text // match "text" literally
// flags:
// "m" - multiline, matches on a per line basis)
Regex101
Edit: If you want to match the suffix tag with the ending quote, you can use this:
/^(?!cat).*(\.\s*text\s*\")/m
Regex101
Notice the match information on the right in the Regex101. The () captures the match to output, assuming the proper use of string/regex methods.
Example of how to extract the captured group using String.prototype.match:
var strArr = [ 'cat.text"',
'cat.doc"',
'dog.txt"',
'dog.text"',
'frog.txt"',
'dog.text"',
'dog. text"',
'cat. text"',
'cat. text "',
'dogtext',
'textexttext'];
var re = /^(?!cat).*(\.\s*text\s*\")/mi;
var testDiv = document.querySelector('.testString');
var resultDiv = document.querySelector('.resultString');
strArr.forEach(function(str) {
testDiv.insertAdjacentHTML('beforeend', str + '<br>');
if (str.match(re))
resultDiv.insertAdjacentHTML('beforeend', str.match(re)[1] + '<br>');
else resultDiv.insertAdjacentHTML('beforeend', '<br>');
});
.testString {
display: inline-block;
}
.resultString {
display: inline-block;
position: relative;
left: 10%;
}
<div class='testString'></div>
<div class='resultString'></div>
I removed the g flag when using string.prototype.match as you want the captured groups. Using the g flag returns the entire match string.
/^(?!cat).*text$/?