简体   繁体   中英

assistance with javascript regexp to replace string

I have some javascript that I need assistance with where I want to update a string with javascript.

Original string:

987654321-200x200-1_This+is+text.jpg

Want it to end up to be:

not_found-200x200.jpg

So 987654321 gets replaced with not_found and -1_This+is+text with nothing.

Note the original string is fully dynamic with only the - x - _ + constant in all.

I have tried something like this:

'987654321-200x200-1_This+is+text.jpg'.replace(/\_\d{0,}[A-Za-z]*/, '_not_found') 

but need help with the regexp to achieve this. Anyone help?

Not sure if this will do, but if all you're looking for is 200x200 you could just split on - and use that :

var str = '987654321-200x200-1_This+is+text.jpg';
var not = 'not_found-' + str.split('-')[1] + '.jpg';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM