There is a string of writing/text in a variable:
var copypaste, replaced;
var verbdb = ["keep","know","enjoy"];
var arrayLength = verbdb.length;
for (var i = 0; i < arrayLength; i++) {
copypaste = "One of the reasons first_name keep waking up at night, is blah blah blah. Try this, let first_name know and see blah blah blah. Remember that first_name enjoy being challenged and blah blah blah.";
replaced = copypaste.replace(RegExp("[^let]? first_name " + verbdb[i] + "(\s|\.)","g")," first_name " + verbdb[i] + "_object_s\$1");
}
What I am seeking to get from the replaced
variable is to exclude ONLY when first_name is preceded by the word let
.
"One of the reasons first_name keep_object_s waking up at night, is blah blah blah. Try this, let first_name know and see blah blah blah. Remember that first_name enjoy_object_s being challenged and blah blah blah.";
So in this example:
_object_s
added in,_object_s
added in.You can try this :
(?:\b)(?!let)(?=\w+\sfirst_name\s+(know|keep|enjoy))(\w+ \w+)\s+(\w+)
const regex = /(?:\\b)(?!let)(?=\\w+\\sfirst_name\\s+(know|keep|enjoy))(\\w+ \\w+)\\s+(\\w+)/g; const str = `One of the reasons first_name keep waking up at night, is blah blah blah. Try this, let first_name know and see blah blah blah. Remember that first_name enjoy being challenged and blah blah blah`; const subst = `$2 _object_s`; const result = str.replace(regex, subst); console.log(result);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.