Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何为以@开头或以、?结尾的字符串匹配编写正则表达式,?_Javascript_Regex_String - Fatal编程技术网

Javascript 如何为以@开头或以、?结尾的字符串匹配编写正则表达式,?

Javascript 如何为以@开头或以、?结尾的字符串匹配编写正则表达式,?,javascript,regex,string,Javascript,Regex,String,如何为以@开头或以,结尾的字符串匹配编写正则表达式。我正在寻找JavaScript代码。对于以@开头或以逗号结尾的字符串,正则表达式如下所示: /^@|,$/ 或者你可以这样做: if ((str.charAt(0) == "@") || (str.charAt(str.length - 1) == ",")) { // string matched } 正则表达式的解决方案是: var rx=/(^@|,$)/; console.log(rx.test(“”);//假的 cons

如何为以
@
开头或以
结尾的字符串匹配编写正则表达式。我正在寻找JavaScript代码。

对于以@开头或以逗号结尾的字符串,正则表达式如下所示:

/^@|,$/
或者你可以这样做:

if ((str.charAt(0) == "@") || (str.charAt(str.length - 1) == ",")) {
    // string matched
}

正则表达式的解决方案是:

var rx=/(^@|,$)/;
console.log(rx.test(“”);//假的
console.log(接收测试(“aaa”);//假的
console.log(rx.test(“@aa”);//真的
console.log(rx.test(“aa,”);//真的

console.log(rx.test(“@a)”);//true
+1表示是唯一解决问题中“或”条件的人。
'@test'.match(/^@.*[^,]$|^[^@].*,$/) // @test
'@test,'.match(/^@.*[^,]$|^[^@].*,$/) // null
'test,'.match(/^@.*[^,]$|^[^@].*,$/) // test,
'test'.match(/^@.*[^,]$|^[^@].*,$/) // null