我有一个字符串:
var string = "aaaaaa<br />† bbbb<br />‡ cccc"
我想用分隔符<br />拆分这个字符串,后面跟着一个特殊字符。
要做到这一点,我使用这个:
string.split(/<br \/>&#?[a-zA-Z0-9]+;/g);
我得到了我需要的东西,只是我丢失了分隔符。 示例如下:http://jsfiddle.net/JwrZ6/1/
如何保留分隔符?
我有一个字符串:
var string = "aaaaaa<br />† bbbb<br />‡ cccc"
我想用分隔符<br />拆分这个字符串,后面跟着一个特殊字符。
要做到这一点,我使用这个:
string.split(/<br \/>&#?[a-zA-Z0-9]+;/g);
我得到了我需要的东西,只是我丢失了分隔符。 示例如下:http://jsfiddle.net/JwrZ6/1/
如何保留分隔符?
当前回答
Most of the existing answers predate the introduction of lookbehind assertions in JavaScript in 2018. You didn't specify how you wanted the delimiters to be included in the result. One typical use case would be sentences delimited by punctuation ([.?!]), where one would want the delimiters to be included at the ends of the resulting strings. This corresponds to the fourth case in the accepted answer, but as noted there, that solution only works for single characters. Arbitrary strings with the delimiters appended at the end can be formed with a lookbehind assertion:
'It is. Is it? It is!'.split(/(?<=[.?!])/)
/* [ 'It is.', ' Is it?', ' It is!' ] */
其他回答
我对jichi的回答做了修改,并把它放在一个支持多字母的函数中。
String.prototype.splitAndKeep = function(separator, method='seperate'){
var str = this;
if(method == 'seperate'){
str = str.split(new RegExp(`(${separator})`, 'g'));
}else if(method == 'infront'){
str = str.split(new RegExp(`(?=${separator})`, 'g'));
}else if(method == 'behind'){
str = str.split(new RegExp(`(.*?${separator})`, 'g'));
str = str.filter(function(el){return el !== "";});
}
return str;
};
jichi的答案第三种方法在这个函数中不起作用,所以我采用了第四种方法,并删除了空白区域以获得相同的结果。
编辑: 第二种方法,除了一个数组来拆分char1或char2
String.prototype.splitAndKeep = function(separator, method='seperate'){
var str = this;
function splitAndKeep(str, separator, method='seperate'){
if(method == 'seperate'){
str = str.split(new RegExp(`(${separator})`, 'g'));
}else if(method == 'infront'){
str = str.split(new RegExp(`(?=${separator})`, 'g'));
}else if(method == 'behind'){
str = str.split(new RegExp(`(.*?${separator})`, 'g'));
str = str.filter(function(el){return el !== "";});
}
return str;
}
if(Array.isArray(separator)){
var parts = splitAndKeep(str, separator[0], method);
for(var i = 1; i < separator.length; i++){
var partsTemp = parts;
parts = [];
for(var p = 0; p < partsTemp.length; p++){
parts = parts.concat(splitAndKeep(partsTemp[p], separator[i], method));
}
}
return parts;
}else{
return splitAndKeep(str, separator, method);
}
};
用法:
str = "first1-second2-third3-last";
str.splitAndKeep(["1", "2", "3"]) == ["first", "1", "-second", "2", "-third", "3", "-last"];
str.splitAndKeep("-") == ["first1", "-", "second2", "-", "third3", "-", "last"];
我一直在用这个:
String.prototype.splitBy = function (delimiter) {
var
delimiterPATTERN = '(' + delimiter + ')',
delimiterRE = new RegExp(delimiterPATTERN, 'g');
return this.split(delimiterRE).reduce((chunks, item) => {
if (item.match(delimiterRE)){
chunks.push(item)
} else {
chunks[chunks.length - 1] += item
};
return chunks
}, [])
}
除了你不该惹斯特恩。这是一个函数版本:
var splitBy = function (text, delimiter) {
var
delimiterPATTERN = '(' + delimiter + ')',
delimiterRE = new RegExp(delimiterPATTERN, 'g');
return text.split(delimiterRE).reduce(function(chunks, item){
if (item.match(delimiterRE)){
chunks.push(item)
} else {
chunks[chunks.length - 1] += item
};
return chunks
}, [])
}
你可以这样做:
var haystack = "aaaaaa<br />† bbbb<br />‡ cccc"
var needle = '<br \/>&#?[a-zA-Z0-9]+;';
var result = splitBy(haystack , needle)
console.log( JSON.stringify( result, null, 2) )
你会得到:
[
"<br />† bbbb",
"<br />‡ cccc"
]
回答它这里也JavaScript分割正则表达式保持分隔符
在正则表达式中使用(?=pattern)前向模式 例子
var string = '500x500-11*90~1+1';
string = string.replace(/(?=[$-/:-?{-~!"^_`\[\]])/gi, ",");
string = string.split(",");
这将得到以下结果。
[ '500x500', '-11', '*90', '~1', '+1' ]
还可以直接拆分吗
string = string.split(/(?=[$-/:-?{-~!"^_`\[\]])/gi);
给出相同的结果
[ '500x500', '-11', '*90', '~1', '+1' ]
使用(正)前向,这样正则表达式就断言特殊字符存在,但实际上并不匹配它:
string.split(/<br \/>(?=&#?[a-zA-Z0-9]+;)/g);
看看它的实际应用:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _bbbb < br / > ‡亿”; 游戏机log(管柱。斯普利特(/ < br \/>(?=&#?[ a-zA-Z0-9] +) / (g));
如果你对拆分模式进行分组,它的匹配将被保留在输出中,这是通过设计的:
如果separator是带有捕获括号的正则表达式,则 每次分隔符匹配时,结果(包括任何未定义的 捕获括号的结果)拼接到输出中 数组中。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split#description
除非您的搜索模式使用前瞻性或全局标志,否则您不需要这些标志。
const str = '如果一只土拨鼠会扔木头,它会扔多少木头?` Const result = str.split(/(\s+)/); console.log(结果); //我们可以验证结果 const isSame = result.join(") === str; console.log({isSame});
您可以使用多个组。你可以尽情发挥你的创造力,小组之外的内容将被删除:
const str = '如果一只土拨鼠会扔木头,它会扔多少木头?` Const result = str.split(/(\s+)(\w{1,2})\w+/); console.log(结果,result.join ("));