0

Hey guys i need to replace a string which starts every time with the same parts. Such as…

var name = $('.item').attr('name'); // Could be »item-name-XYZ« (XYZ) differs each time.
name.replace('item-name-?', 'item-name-newone');

items can appear many times and i do have to replace all names of them. I guess its something with regex…

Thanks in advance.

BTW: My most asked questions are about regex. Does someone has a good source to learn it?

1
  • This question seems odd as formulated, do you wish to change the name attribute of those elements? Otherwise a simple name="item-name-newone"; has the same net effect here. Commented Jun 13, 2013 at 14:29

2 Answers 2

1

Yes, it is about regex. You can learn them here

Your code will look like this:

name.replace(/item-name-(.*)/, 'item-name-newone');
0

You don't need regex for this, you can use javascript

var newone = name.substr(0,10) + 'Xyy';

Your Answer

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

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