Remove part of string that matches text in a variable

I need to remove part of a string that matches the text in a variable. Essentially, I need something like this: $('text').value = $('text').value.replace(/pin,/, '') where pin is a variable containing eight numeric characters and a comma (e.g. 12345678,). $('text').value is a comma-delimited list of PINs.

Thanks.
Shiseiryu1Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
b0lsc0ttConnect With a Mentor IT ManagerCommented:
Shiseiryu1,

If the number is specific then you would need to do something like ...

myregex = new Regexp(variable);
$('text').value = $('text').value.replace(myregex, "");

The "variable" in the first line could be the specific number/comma value you want to remove.  One caution, that "string" would need to be escaped in certain cases but this won't be an issue if you just have numbers and a comma.

Let me know if you have any questions or need more information.

b0lsc0tt
0
 
hieloCommented:
Try:
Single replacement:
$('text').value = $('text').value.replace(/\d{8}[,]/, '')

OR:
Global Replacement:
$('text').value = $('text').value.replace(/\d{8}[,]/g, '')
0
 
Shiseiryu1Author Commented:
Thanks much, b0lsc0tt, that did the trick. I would just add for those who may run across this solution in a search that "Regexp" seems to require intercaps to work correctly, so it has to be "RegExp".

Thanks again!
0
 
b0lsc0ttIT ManagerCommented:
Your welcome!  Good catch and thanks for pointing it out on the spelling error.  Case definitely is important in Javascript and it should've been RexExp.

Thanks for the grade, the points and the fun question.

bol
0
All Courses

From novice to tech pro — start learning today.