javascript - How to write a regex for string matches which starts with @ or end with ,? -



javascript - How to write a regex for string matches which starts with @ or end with ,? -

how write regex string matches starts @ or end ,. looking code in javascript.

regex solution be:

class="snippet-code-js lang-js prettyprint-override">var rx = /(^@|,$)/; console.log(rx.test("")); // false console.log(rx.test("aaa")); // false console.log(rx.test("@aa")); // true console.log(rx.test("aa,")); // true console.log(rx.test("@a,")); // true

but why not utilize string functions first and/or lastly characters:

class="snippet-code-js lang-js prettyprint-override">var strings = [ "", "aaa", "@aa", "aa,", "@a," ]; (var = 0; < strings.length; i++) { var string = strings[i], result = string.length > 0 && (string.charat(0) == "@" || string.slice(-1) == ","); console.log(string, result); }

javascript regex string

Comments

Popular posts from this blog

c# - Move local mssql database to webhosted MYSQL -

java - How to pass parameters between Request-Scoped Controllers? -

string - what is the difference between "some" == "some\0" and strcmp("some","some\0") in c++? -