RegExp to return true only when a character appears once. [Solved] | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

RegExp to return true only when a character appears once. [Solved]

Hi, i've been stuck trying to a solution to this: Eg: var str = "dre3fr@sft"; var str2 = " dre3fr@@sft"; var str3 = "dre3frsft"; I want a regEx that returns true only when a string contains (@) just once: Expected output: regEx.test(str); // true regEx.test(str2); // false regEx.test(str3); // false Thanks in advance.

13th Oct 2020, 8:20 PM
Devotha Mwenda
Devotha Mwenda - avatar
2 Antworten
+ 3
here is what I got that seems to work, I don't honestly think its the best so please correct me if I'm wrong. ^[^@]*@[^@]*$ the [^@] means anything but @.
13th Oct 2020, 9:02 PM
Apollo-Roboto
Apollo-Roboto - avatar
0
Thank you @Apollo-Roboto. That works. I got confused since I was trying to use quantifiers.
15th Oct 2020, 5:29 AM
Devotha Mwenda
Devotha Mwenda - avatar