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

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -