Javascript Reg Expression,缺少字符

时间:2014-09-05 18:06:24

标签: javascript jquery regex

我有一个对话框,在其中我尝试应用正则表达式,我似乎无法找到正确的表达式。

结构

ID          151010  This could be of any length
PartDescr   xyz     This could be of any lenhth
Types       1,2,3,4 These are the index of selection from a listbox, n number and  optional
Comment     xyz     This could be of any lenth and optional
SN          xyz     optional text any length
RC          1       optional drop down index value, only one value

示例数据

151010 ~ Pipettor Aspiration Dispense-Sample Fluidics-Cable/Connections ~ 1,2,3,4 ~ SDSZF ~ S ~ 2 |  151020 ~ Pipettor Aspiration Dispense-Sample Fluidics-Complete Syringe Pump Assy ~ null ~  ~  ~ 1

预期数据

Pipettor Aspiration Dispense-Sample Fluidics-Cable/Connections ~ 1,2,3,4 ~ SDSZF ~ S ~ 2 |
Pipettor Aspiration Dispense-Sample Fluidics-Complete Syringe Pump Assy ~ null ~  ~  ~ 1

正如您在上面所看到的,我只是想在每个部件的开头摆脱ID,可能有N个部件选择。上面有两个,可能是一个,可能是2个,依此类推。我的以下正则表达式语句在删除ID时工作正常,但我注意到对于服务类型,如果我选择了前4个选项,例如1,2,3,4,在正则表达式转换后,我得到1,2,3。所以这是我需要帮助的地方。感谢

partTextAreaNoId = x.replace(/\d+\s~\s/g, "");

6 个答案:

答案 0 :(得分:0)

您可以尝试以下方式:

[^,]\d+\s~\s

哪会排除您的,4 ~子字符串。

或者,您可以尝试:

(^|\|\s+)\d+\s~\s

这将检查您的匹配是从字符串的开头开始,还是|加上一些空格。

答案 1 :(得分:0)

将变量设置为除第一部分之外的拆分结果(取决于您如何拆分)会更简单:

//Where str has been set to the data
str = str.split('|'), result = [], split = null;
for (var i = 0; i < str.length; i++) {
  split = str[i].split('~');
  result.push('~'.join(split[1,split.length - 1]));
}

//Where str has been set to the data
str = str.split('|'), result = [], split = null;
for (var i = 0; i < str.length; i++) {
  split = str[i].split(' ');
  result.push(' '.join(split[2,split.length - 1]));
}

答案 2 :(得分:0)

不需要正则表达式,只需要一点点拆分,indexOf和substring - (UPDATED)http://jsfiddle.net/q84vtfa5/1/

var myData = '151010 ~ Pipettor Aspiration Dispense-Sample Fluidics-Cable/Connections ~ 1,2,3,4 ~ SDSZF ~ S ~ 2 |  151020 ~ Pipettor Aspiration Dispense-Sample Fluidics-Complete Syringe Pump Assy ~ null ~  ~  ~ 1';
var arrayData = myData.split('|');
for(i = 0; i < arrayData.length; i++) {
    var idStripped = arrayData[i].substring(arrayData[i].indexOf('~') + 2);
    console.log(idStripped);
}

这会在第一个代字号的位置开始子字符串,再加上两个字符串,以便剩下的内容从您的产品说明开始。

答案 3 :(得分:0)

我想你想要这样的东西,

> "151010 ~ Pipettor Aspiration Dispense-Sample Fluidics-Cable/Connections ~ 1,2,3,4 ~ SDSZF ~ S ~ 2 |  151020 ~ Pipettor Aspiration Dispense-Sample Fluidics-Complete Syringe Pump Assy ~ null ~  ~  ~ 1".replace(/((?!,)(?:^|.))\s*\d+\s~\s/g, "$1");
'Pipettor Aspiration Dispense-Sample Fluidics-Cable/Connections ~ 1,2,3,4 ~ SDSZF ~ S ~ 2 |Pipettor Aspiration Dispense-Sample Fluidics-Complete Syringe Pump Assy ~ null ~  ~  ~ 1'

<强>更新

> "151010 ~ Pipettor Aspiration Dispense-Sample Fluidics-Cable/Connections ~ 1,2,3,4 ~ SDSZF ~ S ~ 2 |  151020 ~ Pipettor Aspiration Dispense-Sample Fluidics-Complete Syringe Pump Assy ~ null ~  ~  ~ 1".replace(/\s*\d+\s~\s(?![^~]*~[^~]*~[^~]*(?:\||$))/g, "");
'Pipettor Aspiration Dispense-Sample Fluidics-Cable/Connections ~ 1,2,3,4 ~ SDSZF ~ S ~ 2 |Pipettor Aspiration Dispense-Sample Fluidics-Complete Syringe Pump Assy ~ null ~  ~  ~ 1'

答案 4 :(得分:0)

尝试使用此尺寸:

x.replace(/(^|\|\s+)\d+\s+~\s+/g, "$1");

由于你的正则表达式只需要更多的上下文 - 我告诉它管道字符是你的记录分隔符,并从记录中替换出第一个数字,波浪号和空格。

用于:

"151010 ~ Pipettor Aspiration Dispense-Sample Fluidics-Cable/Connections ~ 1,2,3,4 ~ SDSZF ~ S ~ 2 |  151020 ~ Pipettor Aspiration Dispense-Sample Fluidics-Complete Syringe Pump Assy ~ null ~  ~  ~ 1"

返回:

"Pipettor Aspiration Dispense-Sample Fluidics-Cable/Connections ~ 1,2,3,4 ~ SDSZF ~ S ~ 2 |  Pipettor Aspiration Dispense-Sample Fluidics-Complete Syringe Pump Assy ~ null ~  ~  ~ 1"

答案 5 :(得分:0)

你可以使用这个正则表达式:

^\d+\s~\s([\w\d\s-\/]+)\s~\s((?:\d,?)*)\s~\s([\w\d\s-\/]+)?\s~\s([\w\d\s-\/]+)?\s~\s(\d*)

这里有一些你可以使用的javascript代码:

var re = /^\d+\s~\s([\w\d\s-\/]+)\s~\s((?:\d,?)*)\s~\s([\w\d\s-\/]+)?\s~\s([\w\d\s-\/]+)?\s~\s(\d*)/; 
var str = '151010 ~ Pipettor Aspiration Dispense-Sample Fluidics-Cable/Connections ~ 1,2,3,4 ~ SDSZF ~ S ~ 2';
var m;

while ((m = re.exec(str)) != null) {
    if (m.index === re.lastIndex) {
        re.lastIndex++;
    }
}

如果您想查看结果或捕获群组,您可以使用m[0]之类的m变量,这是第一个被捕获的群组PartDescr,而m[1]Types,等等。 只要告诉我你是否想要对正则表达式中使用的每一位进行详细解释,我很乐意提供帮助。

希望它有用!