3

There is some text, exp: "The string class is an instantiation of the basic_string class template that uses char".

I need to find the text - "basic_string", but if there is no word "the" in front of him.

If use negative lookbehind, it was be:

(?<!\sthe)\s+basic_string

But javascript not understand negative lookbehind, what to do?

1

4 Answers 4

3

If the only allowed character between "the" and "basic_string" is the white-space:

([^e\s]|[^h]e|[^t]he)\s+basic_string
Sign up to request clarification or add additional context in comments.

3 Comments

I saw this question, and thought it a particularly good one. Not sure exactly what's going on here, but this solution works. It can be tested here: regex101.com/r/vX5rJ5/1
(^|[^e\s]|(^|[^h])e|(^|[^t])he|\wthe)\s+basic_string covers even those particular cases.
1

You can use xregexp library to get advanced regex features like lookbehind in Javascript.

Alternatively you can use alternation and capture group as a workaround:

var s = 'The string class is an instantiation of the basic_string class template that uses char';

var kw = s.match(/\bthe basic_string\b|(\bbasic_string\b)/)[1];
// undefined

s = 'instantiation of basic_string class template'
kw = s.match(/\bthe basic_string\b|(\bbasic_string\b)/)[1]
//=> "basic_string"

In this regex, captured group #1 will only be populated if bbasic_string isn't preceded by word the.

Comments

1

You can use RegExp /(the)(?\sbasic_string)/ or new RegExp("(" + before + ")(?=" + match + ")") to match "the" if followed by " basic_string", .match() to retrieve .index of matched string, .slice() to get "basic_string"

var str = "The string class is an instantiation of the basic_string class template that uses char";
var before = "the";
var match = " basic_string";
var index = str.match(new RegExp("(" + before + ")(?=" + match + ")")).index 
            + before.length + 1;
console.log(str.slice(index, index + match.length));

Comments

1

The easiest way to emulate the negative lookbehind is via an optional capturing group, and check if the group participated in the match:

/(\bthe)?\s+basic_string/g
 ^^^^^^^^

See this JS demo:

var s = 'The string class is an instantiation of the basic_string class template that uses char, not basic_string.';
var re = /(\bthe)?(\s+basic_string)/gi;
var res = s.replace(re, function(match, group1, group2) {
   return group1 ? match : "<b>" + group2 + "</b>";
});
document.body.innerHTML = res;

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.