Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace strings from left until the first occuring string in Javascript

I am trying to figure out how to replace example:

sw1_code1_number1_jpg --> code1_number1_jpg
hon2_noncode_number2_jpg --> noncode_number2_jpg
ccc3_etccode_number3_jpg --> etccode_number3_jpg
ddd4_varcode_number4_jpg --> varcode_number4_jpg

So the results are all string after the first _ If it doesn't find any _ then do nothing.

I know how to find and replace strings, str.replace, indexof, lastindexof but dont know how remove up to the first found occurrence.

Thank You

like image 326
Erjon G. Avatar asked Oct 20 '22 06:10

Erjon G.


1 Answers

Use the replace method with a regular expression:

"sw1_code1_number1_jpg".replace(/^.*?_/, "");
like image 68
Robby Cornelissen Avatar answered Oct 23 '22 04:10

Robby Cornelissen