sapphirecloud sapphirecloud - 1 month ago 9
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

You can use

^((?:[^{\n])+)({{|{:)*

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

See https://regex101.com/r/3UOewQ/4 that I used to verify, that it works

Comments