Ayush Bahuguna Ayush Bahuguna - 1 month ago 9
Javascript Question

Regex test should return true only when pattern matches throughout the string. Javascript

I really didn't how to put it in a form of question, but I have to write a function that matches a string for a pattern

+a+
.

the string should be of format where each alphabet is surrounded by "+" sign.

I came up with this expression,
var letter = /\+\w\+/;
. The problem with this is that it returns true even if the pattern matches just once throughout the string.

for example, it returns true for
"+f+++a+b+c++"
, but it returns true for
"+f=++a+b+c++"
also.

Thank You

Answer

You can match the reverse and throw the error

Something like

"+f+++a=+b+=c++".match(/(\w=|=\w)/g) == null ; //false

and

"+f+++a+b+c++".match(/(\w=|=\w)/g) == null ; //true
Comments