我有一个字符串:

var string = "aaaaaa<br />&dagger; bbbb<br />&Dagger; cccc"

我想用分隔符<br />拆分这个字符串,后面跟着一个特殊字符。

要做到这一点,我使用这个:

string.split(/<br \/>&#?[a-zA-Z0-9]+;/g);

我得到了我需要的东西,只是我丢失了分隔符。 示例如下:http://jsfiddle.net/JwrZ6/1/

如何保留分隔符?


当前回答

回答它这里也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 / > &Dagger;亿”; 游戏机log(管柱。斯普利特(/ < br \/>(?=&#?[ a-zA-Z0-9] +) / (g));

我一直在用这个:

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 />&dagger; bbbb<br />&Dagger; cccc"
var needle =  '<br \/>&#?[a-zA-Z0-9]+;';
var result = splitBy(haystack , needle)
console.log( JSON.stringify( result, null, 2) )

你会得到:

[
  "<br />&dagger; bbbb",
  "<br />&Dagger; cccc"
]

我遇到了类似但略有不同的问题。无论如何,这里有三种不同场景的示例,用于说明在何处保存分隔符。

"1、2、3".split("、") == ["1", "2", "3"]
"1、2、3".split(/(、)/g) == ["1", "、", "2", "、", "3"]
"1、2、3".split(/(?=、)/g) == ["1", "、2", "、3"]
"1、2、3".split(/(?!、)/g) == ["1、", "2、", "3"]
"1、2、3".split(/(.*?、)/g) == ["", "1、", "", "2、", "3"]

警告:第四个只适用于拆分单个字符。ConnorsFan提供了一个替代方案:

// Split a path, but keep the slashes that follow directories
var str = 'Animation/rawr/javascript.js';
var tokens = str.match(/[^\/]+\/?|\//g);

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!' ] */

回答它这里也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' ]