我需要&
保留在字符串中。
例:
"Rajagiri School of Engineering & Technology;Indian School of Mines University;"
输出应为:
['Rajagiri School of Engineering & Technology', 'Indian School of Mines University']
最佳答案
您可以将replace
与回调一起使用,并丢弃&
之前存在;
的结果。
var str = "Rajagiri School of Engineering & Technology;Indian School of Mines University;";
var arr = str.replace(/(&)?;/g, function($0, $1) { return $1=="&"? $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/