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?
asked Jun 13, 2013 at 13:47
YeppThat'sMe
1,8626 gold badges30 silver badges47 bronze badges
2 Answers 2
Yes, it is about regex. You can learn them here
Your code will look like this:
name.replace(/item-name-(.*)/, 'item-name-newone');
answered Jun 13, 2013 at 13:50
Niccolò Campolungo
12k4 gold badges36 silver badges40 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You don't need regex for this, you can use javascript
var newone = name.substr(0,10) + 'Xyy';
Comments
lang-js
name="item-name-newone";has the same net effect here.