sapphirecloud sapphirecloud - 1 year ago 99
Javascript Question

Javascript regular expression match anything but

I want a regular expression that matches anything but '{{' or '{:'

test cases:

var t1= abcd;
var t2= abcd{{;
var t3= abcd{:efg;

I want them to have the output when using t.match(REGEX):

//"abcd".match(REGEX) --> ["abcd"];
//"abcd{{.match(REGEX)"--> ["abcd","{{"]
//"abcd{:efg".match(REGEX)--> ["abcd","{:"]

I've tried :

///^[\s\S]+[^{{|{:]/ //the the match returns null
///^[\s\S]*?(?={{|{:)/ //but for t1-->matches null
///^[\s\S]+[^{{|{:]+[\s\S]*/ //but for t3--> returns abcd{{efg

but they always return:

//"abcd".match(REGEX) --> null

Is there a way for it to return ["abcd"] for even if there is no "{{" or "{:" character?

Answer Source

You can use


((?:[^{\n])+) - first group before new line or { ,
({{|{:)* - optional {{ or {: group.

See that I used to verify, that it works

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download