我需要&保留在字符串中。

例:

"Rajagiri School of Engineering & Technology;Indian School of Mines University;"


输出应为:

['Rajagiri School of Engineering & Technology', 'Indian School of Mines University']

最佳答案

您可以将replace与回调一起使用,并丢弃&amp之前存在;的结果。

var str = "Rajagiri School of Engineering & Technology;Indian School of Mines University;";

var arr = str.replace(/(&amp)?;/g, function($0, $1) { return $1=="&amp"? $1+";" : "\n";
          }).split("\n").filter(Boolean);


输出:

["Rajagiri School of Engineering & Technology",
 "Indian School of Mines University"]

关于javascript - javascript正则表达式按分号分隔,但&,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36764840/

10-12 00:50