简体   繁体   English

使用正则表达式分割字符串-javascript

[英]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. 在上面的字符串中,第一个是化学名称,第二个是其编号no,最后一个是其在该产品中的百分比。因此,我想按以下方式拆分这些字符串,以便对我的页面有帮助差异部分有不同的字段。

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 演示版

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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