Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript regex to find words that do not start with "my:"

I'm trying to write a regex that will find all values between curly braces that do not begin with "my:". For example, I want to capture {this} but not {my:monkey}.

The pattern that captures everything is:

\{([^\}]*)\}

I'm having trouble getting it to work. My closest shot so far is:

\{[^my:]*([^\}]*)\}

This fails because it only ignores tags beginning with "m", "y" or ":".

I'm sure there is a command I'm overlooking to treat "my:" as a block..

(Note: Must work for Javascript)

like image 465
Anthony Avatar asked Jul 19 '13 22:07

Anthony


People also ask

Does not start with regex JS?

To check if a string does not start with specific characters using a regular expression, use the test() function and negate it. Make sure your regular expression starts with ^ , which is a special character that represents the start of the string.

What is \b in regex JavaScript?

The RegExp \B Metacharacter in JavaScript is used to find a match which is not present at the beginning or end of a word. If a match is found it returns the word else it returns NULL. Syntax: /\B/ or new RegExp("\\B") Syntax with modifiers: /\B/g.

How do you regex only words?

To run a “whole words only” search using a regular expression, simply place the word between two word boundaries, as we did with ‹ \bcat\b ›. The first ‹ \b › requires the ‹ c › to occur at the very start of the string, or after a nonword character.

How do I check a string in regex?

Use the test() method to check if a regular expression matches an entire string, e.g. /^hello$/. test(str) . The caret ^ and dollar sign $ match the beginning and end of the string. The test method returns true if the regex matches the entire string, and false otherwise.


3 Answers

This one should do:

/\{((?!my:)[^}]+)\}/g

Check quick demo http://jsbin.com/ujazul/2/edit

like image 62
elclanrs Avatar answered Oct 27 '22 03:10

elclanrs


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

like image 3
o0omycomputero0o Avatar answered Oct 27 '22 01:10

o0omycomputero0o


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}
like image 2
p.s.w.g Avatar answered Oct 27 '22 03:10

p.s.w.g