Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Split string in two by splitting it at the last '_' symbol

I have this structure of string 'amount_capital_expected_eighty_percent_6009a08948abf6001e95359a'. I would like to cut the part after the last '_' from the rest of the string with a String.split() function.

So the result would be:

['amount_capital_expected_eighty_percent', '6009a08948abf6001e95359a']

My attempt was this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const idIndex = formValue.split(/_(?=.+)$/);

but this cuts it too soon. Someone knows the solution to this?

>Solution :

You could split on underscore followed by a negative lookahead asserting that no further underscores appear in the string:

var formValue = "amount_capital_expected_eighty_percent_6009a08948abf6001e95359a";
var parts = formValue.split(/_(?!.*_)/);
console.log(parts);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading