If I have a string like
${employee} and ${} should be validate
And I want to fetch all substring that contains the pattern like
${}
and validate as if string starting with ${ and } must have value?
suppose for below string
${employee} and ${} should be validate
it should return array with two element
[ ${employee}, ${} ]
and upon validate it should show the second element as invalid (as it is blank)
for this, I have tried the following code
function fetchMatches(theString, theRegex){
return theString.match(theRegex).map(function(el) {
var index = theString.indexOf(el);
return [index, index + el.length - 1];
});
}
fetchMatches(" ${employee} and ${} should be validate",/(\$)(\{)(.*)(\})/i);
but it is not returning the desired output.
guys, please suggest some help
You may use the following solution: use
/\${[^{}]*}/g
regex withString#match
to fetch all the matches, and then loop over the matches to check which have an empty value:Since you mentioned that there can be nested values you may use
XRegExp
: