2

I am new using regular expression and I was wondering if it is possible to exclude a word. I have the following list of data:

cat.text"
cat.doc"
dog.txt"
dog.text"
frog.txt"
dog.text"
dog.  text"
cat. text"
cat. text "

From this data, I want to get only the .text" (or . text ", or .text ", etc.) part but I don't want to include the .text" from any item that contains cat. So far, I am able to get all the .text" but not sure how to exclude the ones with cat word. I have so far \.\s*text\s*" and it gets

cat.text"  <--
cat.doc"
dog.txt"
dog.text"   <--
frog.txt"
dog.text"   <--
dog.  text"  <--
cat. text"    <--
cat. text "   <--

Any help? Thanks!

1
  • how about /^(?!cat).*text$/? Commented May 12, 2016 at 1:34

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

hi @timolawl, thanks for your answer!. This solution is in the right direction! I would like to get only the .text" . text" . text " . If I use /^(?!cat).*\.\s*text"/gm I get the dog.text" for example. Is it possible to only get the .text" from the result of your regexp? Thanks in advance
@zancudo Just to clarify, you want to match the extension with the ending quote? I'll update my answer. Give me a moment.
If there's no ending quote, you can just remove the \" in the updated regex above.
Thanks for your information @timolawl. Sorry that I edited my comment after you posted you answer again. I was wondering if it is possible to only get the .text" part from the result of your regexp? I mean that instead of get dog.text" for example, I can only get the .text" part of each item that matches in your result?
You should be able to use string.prototype.match to get the captured group. I'll update my answer to show you what I mean.
0

you can do something like this...

  1. split the string into two parts
  2. use one of the parts to check if you'll exclude (or include) that
  3. return the other part (or both if you want)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.