1

How to remove special characters from string. If user provided any special character from the below set of characters that should be removed:

| \ " ' / [ ] : < > + = , ; ? * @

I tried regex. But it is not replacing. Please find below code:

var subSiteTitleval=$("input[title='Subsite Title Required Field']").val();

subSiteTitleval= subSiteTitleval.replace(/[@><,:;][+=?"'*/|]/g, ' ');

$("input[title='Subsite Title Required Field']").val(subSiteTitleval);
});

Kindly respond.

0

Can you try this?

var subSiteTitleval=$("input[title='Subsite Title Required Field']").val();

subSiteTitleval= subSiteTitleval.replace(/[^a-zA-Z0-9-. ]/g, ' ');

$("input[title='Subsite Title Required Field']").val(subSiteTitleval); });

If you don't want to remove some of the special characters, just add inside regex like this

/[^a-zA-Z0-9-. &]/g

You can refer to this

Not the answer you're looking for? Browse other questions tagged or ask your own question.