1

Suppose here is a sample text:

Hello this is testing _testisgood _test test ilovetesting again test

The regex

/test/gi

Gives all the test but I only want the test string which is surrounded by some other character except space means the opposite of exact match. In other words the test in testing , _testisgood ,ilovetesting i want to match.

2
  • 2
    how about _test? it doesn't come after whitespace Commented Dec 24, 2021 at 19:50
  • You said you "only want the test string which is surrounded by some other character except space means the opposite of exact match." Considering that "testing" is a match, surrounded seems like the wrong word and I don't know what "opposite of exact match" means (an "inexact match"?). You need to state the problem more precisely, such as "I wish to match 'test' when it is (not preceded by a ???) and is (not followed by a ???)" or ".... when it is (preceded by a ???) and is (followed by a ???)". Commented Dec 25, 2021 at 1:18

6 Answers 6

2

Bill's answer is good but may you like this one: just find all words with test and then filter out useless ones;

const s = "Hello this is testing _testisgood _test test ilovetesting again test"
 
console.log(
    (s.match(/[^\s]*test[^\s]*/gi) || []).filter(s => s !== 'test')
)
Sign up to request clarification or add additional context in comments.

Comments

1

The regex below will match 'test' when it either has a non-whitespace character(s) prefixing or post fixing it.

/([^\s]+test[^\s]*|[^\s]*test[^\s]+)/gi;

OR

/(\S+test\S*|\S*test\S+)/gi;

const sentence = "Hello this is testing _testisgood _test test ilovetesting again test";

regex = /([^\s]+test[^\s]*|[^\s]*test[^\s]+)/gi;

console.log(sentence.match(regex));

Comments

0

You can match just _testisgood and ilovetesting in your example by specifying one or more characters that are not whitespace before and after test, like this:

/[^\s]+test[^\s]+/gi

If you also want to match testing, then drop [^\s]+ from the beginning of the pattern.

3 Comments

It is not detecting test in testing
@s_guha96 See my update.
I have modified this [^\s]+test[^\s]+|[^\s]+test|test[^\s]+ if it have not whitespace on either sie then take it
0

should still work like this \btest\w+|\w+test\b|\w+test\B\w+

1 Comment

It would help if you explained what this does
0

Since a "word" in your scenario is a chunk of one or more non-whitespace chars (and a non-whitespace char is matched with \S in regex) you can use

console.log(
  "Hello this is testing _testisgood _test test ilovetesting again test"
    .match(/\S+test\S*|\S*test\S+/gi))
// => ["testing","_testisgood", "_test", "ilovetesting"]

Here, \S+test\S*|\S*test\S+ (see this regex demo) matches either

  • \S+test\S* - one or more non-whitespace chars, test and zero or more non-whitespace chars
  • | - or
  • \S*test\S+ - zero or more non-whitespace chars, test and one or more non-whitespace chars.

Or, you can split with any one or more whitespace chars (with .split(/\s+/)) and then filter out any chunk that is equal to test string or does not contain test string:

console.log(
  "Hello this is testing _testisgood _test test ilovetesting again test".split(/\s+/)
    .filter(x => x.toLowerCase() != "test" && x.toLowerCase().includes("test")))

Considering your expected output it may seem that you want to match any non-whitespace chunk that contains test and at least one more letter. In that case, you can use

console.log(
  "Hello this is testing _testisgood _test test ilovetesting again test"
    .match(/[^\sa-zA-Z]*[a-zA-Z]\S*test\S*|\S*test[^\sa-zA-Z]*[a-zA-Z]\S*/gi))
// => ["testing","_testisgood", "_test", "ilovetesting"]

See this regex demo. Output: ["testing", "_testisgood", "ilovetesting"]

Details:

  • [^\sa-zA-Z]* - any zero or more chars other than whitespace and letters
  • [a-zA-Z] - a letter
  • \S*test\S* - test enclosed with zero or more non-whitespace chars
  • | - or
  • \S*test[^\sa-zA-Z]*[a-zA-Z]\S* - zero or more non-whitespace chars, test, any zero or more chars other than whitespace and letters and then a letter and zero or more non-whitespace chars.

Comments

0

If what you want is:

Gives all the test but I only want the test string which is surrounded by some other character

Let's do it with another approach string built-in functions, instead of Regex

function surroundedBy(string, wordInMiddle){
  const allWords = s.split(/\s+/);
  const wordsSurrounded = allWords.filter(word=>word.toLowerCase().includes(wordInMiddle) && 
                                                        !word.startsWith(wordInMiddle) && 
                                                        !word.endsWith(wordInMiddle))
  return wordsSurrounded;
} 

Test:

const s = "Hello this is testing _testisgood _test test ilovetesting again test"
console.log(surroundedBy(s,'test'))

Result: ['_testisgood', 'ilovetesting']

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.