简体   繁体   English

Javascript正则表达式-删除字符,空格和开头0

[英]Javascript Regex - Remove Chars, WhiteSpace & Starting 0

I have hit a brick wall with this one.... 我已经用这个砸了墙。

I have managed to remove the Chars, but looking for the removal of all WhiteSpaces, Carrige Returns & the 0's at the start of each entry after the Chars have been removed. 我设法删除了字符,但是在删除字符后,希望在所有条目的开头都删除所有空白,Carrige Return和0。

var p_string = 'OPS010001 BLW020002 MKS030003';
var p_data = p_string.split(/[^0-9]+/g);

$('body').html(p_data + '<BR>');

JSFiddle JSFiddle

Use string.replace function. 使用string.replace函数。

string.replace(/^0|\s/g, "");

\\s matches white space characters including line breaks. \\s匹配空格字符,包括换行符。

You want something like this 你想要这样的东西

var p_data = p_string.split(/[^0-9]+/g).map(function(x){

  return x.replace(/^0|\s/g, ""); // remove space, carriage return and 0 at start

}); // you can use Array.join to join the array with separator of your choice

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

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