0

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"

1
  • what exactly do classString and className contain? Commented Nov 30, 2011 at 15:07

1 Answer 1

4

I think you need to escape the backslashes inside the search string -

var regex = new RegExp(className+"\\[(.*?)\\]");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.