0

I want to make my alphanumeric regex dynamic in a way that it takes the allowed special characters with it as an argument by the user. Following is my code ... I am getting quotes error here .... any body can tell me how to go about it ?

function aplhanumeric(value,allowed){

///^[a-z0-9_\-]+$/i
alert(allowed);
if(allowed != ''){
    var regex = new RegExp('/^[a-z0-9_\' + allowed + ']+$/i');
    return (value.match(regex));
}else{
    return (alphaNumericRegex.test(value));
}
}

1 Answer 1

1

You've actually escaped the quote, so you have to escape the escape

var regex = new RegExp("^[a-z0-9_\\" + allowed + "]+$", "i");
Sign up to request clarification or add additional context in comments.

4 Comments

@Adi's - I just changed it, try it now.
I actually want it to accept alphanumeric with allowed characters at any place in the string ?
Then why are you using ^ and $ that checks the entire string? Explain exactly what you're trying to do ?
I want to pass all allowed special characters in a variable namely allowed which would hold values such as "." then the regex would be built which would allow aplhanumeri with special characters only "" and "."

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.