简体   繁体   中英

Splitting a string using regular expression -javascript

I have the following two strings

Ethanethiol, 2,2'-thiobis- Not available. 1 - 5
ethyl acetate 109159-22-0 1 - 5

In those above strings first one is the chemical name , next to it is its id no and last one is its percent in that product.So I want to split these strings in the below manner so that it will be helpful for me for the page where there are different fields for diff section.

I want to split these strings like this

Ethanethiol, 2,2'-thiobis- 
Not available.   
1 
5
ethyl acetate
109159-22-0
1
5

I do not want to take any string variable.The code should be dynamic.I mean just input the string in those above format it will show the output as below.Here is my code for the second one

(function(){
 x=prompt("Enter Data");
re=/([\D\d]*)([\d-]+)\s*(\d*\s*\.?\s*\d*)\s*-\s*(\d*\s*\.?\s*\d*)/;
alert("[0]"+x.match(re)[0]);alert("[1]"+x.match(re)[1]);alert("[2]"+x.match(re)[2]);
alert("[3]"+x.match(re)[3]);alert("[4]"+x.match(re)[4]);})();

But as in first one there is nos so it is not working for the code.Any Suggestions please?

use this pattern

\s\s+|\s-\s  

Demo

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