4

I am awful at regex, and have been trying to teach myself.

In order to practice I am trying to remove all numbers and an underscores from a string.

I have been able to remove the numbers with the following, but the combination of both is bamboozling me.

var name = $(this).attr('id').replace(/\d+/g, '');

I am not sure how to combine the two. The following is one of my many efforts but to no avail.

var name = $(this).attr('id').replace(/\d+/\/_/g, '');

2 Answers 2

4

You need to use a character class [\d_] that will match a digit or an underscore.

Here is a small demo:

console.log("Some_1234_6789 demo.".replace(/[\d_]+/g, ''))

Sign up to request clarification or add additional context in comments.

Comments

0

Also Use this

$(this).attr('id').replace(/_|\d+/g,'');

or

$(this).attr('id').replace(/[_\d]+/g,'')

Comments

Your Answer

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