简体   繁体   中英

Regular expression in JavaScript that matches ${}

I want regular expressions to match string that starts with either ${ , "${ , '${ and ends with } , }' , }". My string may be ${anythinghere}, "${anythinghere}", '${anythinghere}'. I tried with the following

var str = "${a}";
var patt = /^("|'|${)*(}|'|")$/;
var res = patt.test(str);
alert(res);

But my above code always returns false.

You have to escape the inner $ with a backslash, since it's a special character. But that alone won't fix the problem. As it is above, patt matches any string that begins with " or ' or ${ , 0 or more times, and ends with } or ' or " . The regular expression that you want would be something like this:

var str = "${a}";
var patt = /^(['"]?)\${.*}\1$/;
var res = patt.test(str);
alert(res);

Here is what each part of patt is doing:

  • ^(['"]?) : The string must begin with 0 or 1 single quote, or with 0 or 1 double quote. This is in parentheses so that we can reference it at the end of the regexp
  • \\${ : Next must be a dollar sign followed by an open curly bracket
  • .* : Next must be 0 or more of any character (other than a newline)
  • } : Next must be a closed curly bracket
  • \\1$ : Finally, the string must end with whatever pattern was matched at the beginning of the string. \\1 is a "back-reference" to the first capturing group (in the parentheses), so if the string began with a single quote, it will only match if it also ends with a single quote. Same goes for double quotes, and no quotes at all

$ has a special meaning in regex, so it must be escaped using a backslash \\ . If you want 1 or more wildcards, that's represented with a .+ , not a * . The following code will match the test pattern:

/^'?"?\${.+}'?"?$/

This works:

var str = "\"${a}\"";
var patt = /^\"\$\{((?!\").)+\}\"$/;
var res = patt.test(str);
alert(res);

Note that the value assigned to str has been changed to \\"${a}\\" in order to reflect what you stated targets. You must escape (put a backslash before) any character you wish to be read literally and not as a metacharacter. In order to have this match any one of your targets at once, simply repeat the pattern in patt three times, separated by pipes as usual, replacing the \\" with \\' or nothing at all (in which case, you should change the \\" inside the inner most parenthesis to }). These changes are shown below:

var patt = /^\"\$\{((?!\").)+\}\"$|^\'\$\{((?!\').)+\}\'$|^\$\{((?!\}).)+\}$/;
You can use
var a=new RegExp("\^(\'\\$\{)[a-z]+(\}\')|(\"\\$\{)[a-z]+(\}\")|(\\$\{)[a-z]+(\})\$");
a.test("${vijay}")//true
a.test("\'${vijay}\'")//true
a.test("\"${vijay}\"")//true
a.test("\'${vijay}\"")//false

If you use only \$ it takes as end og expresion.So use\\$ means $ as a special character

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM