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

How to extract specific words from a string with some patterns?

I am trying to extract some strings from a word with some pattern like –

"38384-1-page1-2222", "1-22-page33-02", "99-222-frontpage-111"

how will I extract all word between - separately, means first word before - and then second word between - and - and so on…

string = "38384-1-page1-2222";
string.substr(0, string.indexof("-")); //return 38384

But how will I extract 1, page1 and 2222 all the words separately?

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

>Solution :

The javascript function str.split(separator) split the string by the given separator and it returns an array of all the splited string. REF Here

Here is an example following your question :

var string = "38384-1-page1-2222";
var separator = "-";

var separated = string.split(separator);

var firstString = separated[0];  // will be '38384'
var secondString = separated[1]; // will be '1'
var thirdString = separated[2];  // will be 'page1'
/* And So on ... */
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