简体   繁体   English

JavaScript中与$ {}匹配的正则表达式

[英]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}'. 我的字符串可能是$ {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. 但是我上面的代码总是返回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 " . 如上所述, patt匹配以"'${开头,0次或更多次,以}'"结尾的任何字符串。 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: 这是patt每个部分正在做的事情:

  • ^(['"]?) : 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 ^(['"]?) :字符串必须以0或1单引号或0或1双引号开头。在括号中,以便我们可以在正则表达式的末尾引用它
  • \\${ : 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) .* :下一个必须为0或更大的任何字符(换行符除外)
  • } : 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$ :最后,字符串必须以在字符串开头匹配的任何模式结尾。 \\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. \\1是第一个捕获组的“反向引用”(在括号中),因此,如果字符串以单引号开头,则仅当字符串也以单引号结尾时才匹配。 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 * . 如果要使用1个或多个通配符,则以.+而不是* 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. 请注意,分配给str的值已更改为\\“ $ {a} \\”,以反映您指定的目标。 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 }). 为了一次与您的任何一个目标匹配,只需重复3次patt模式,并照常用管道隔开,用\\代替\\“或根本不替换(在这种情况下,您应该更改\\ “}内的最括号内。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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