Javascript regex to find words that do not start with "my:"
Summarize as following:
// test match thing_done but not some_thing_done (using nagative lookbehind)
console.log(/(?<!some_)thing_done/.test("thing_done")); // true
console.log(/(?<!some_)thing_done/.test("some_thing_done")); // false
// test match thing_done but not think_done_now (using nagative lookahead)
console.log(/thing_done(?!_now)/.test("thing_done")); // true
console.log(/thing_done(?!_now)/.test("thing_done_now")); // false
// test match some_thing_done but not some_thing (using positive lookbehind)
console.log(/(?<=some_)thing_done/.test("thing_done")); // false
console.log(/(?<=some_)thing_done/.test("some_thing_done")); // true
// test match thing_done but not think_done_now (using positive lookahead)
console.log(/thing_done(?=_now)/.test("thing_done")); // false
console.log(/thing_done(?=_now)/.test("thing_done_now")); // true
Dialogue version:
I need match some_thing_done not thing_done:
Put `some_` in brace: (some_)thing_done
Then put ask mark at start: (?some_)thing_done
Then need to match before so add (<): (?<some_)thing_done
Then need to equal so add (<): (?<=some_)thing_done
--> (?<=some_)thing_done
?<=some_: conditional back equal `some_` string
Link example code: https://jsbin.com/yohedoqaxu/edit?js,console
This one should do:
/\{((?!my:)[^}]+)\}/g
Check quick demo http://jsbin.com/ujazul/2/edit
You can do something like this:
var input = "I want to capture {this} but not {my:monkey}";
var output = input.replace(/{(my:)?([^}]*)}/g, function($0, $1, $2) {
return $1 ? $0 : "[MATCH]";
});
// I want to capture [MATCH] but not {my:monkey}