Reputation: 1668
I am trying to create a function that will pass some parameters to a regex script:
function classAttributes( classString, className ) {
var data;
var regex = new RegExp(className+"\[(.*?)\]");
var matches = classString.match(regex);
if ( matches ) {
//matches[1] refers to options inside [] "required, email, ..."
var spec = matches[1].split(/,\s*/);
if ( spec.length > 0 ) {
data = spec;
}
}
return data;
}
but for some reason it doesnt like the string variable that I pass it "new RegExp(className+"[(.*?)]");" it doesnt throw an error but the validation doesnt work.
Edit: I will take the information from the class stribute and pass it as classString
<div class="field-character-count test[asd, 123, hello]"></div>
and the "className" will represent "test"
Upvotes: 0
Views: 360
Reputation: 24236
I think you need to escape the backslashes inside the search string -
var regex = new RegExp(className+"\\[(.*?)\\]");
Upvotes: 4