与所有空格匹配的Regex,但:
\sand\s\sor\s\sbut\s,\s输入
字体重量、宽度和高度、背景或背景色,但转换样式
input.replace(regex,"-")
匹配
字体大小、宽度和高度、背景或背景颜色,但转换样式
取代
字体-重量、宽度和高度、背景或背景-颜色,但转换-样式
输出
字体重量、宽度和高度、背景或背景色,但转换样式。
发布于 2017-09-28 04:04:01
因为JavaScript不支持向后看,所以您将找不到一个正则表达式,它可以像替换一样简单地完成它。如果您愿意稍微扩展替换逻辑,您可以使用如下的答案(在这里改编自答案:Regex negative lookbehind not valid in JavaScript)
var input = " font weight, bland food, width and height, background, or background color, but transform style, and background repeat"
var regex = /\b(\s?and|\s?but|\s?or|,)?\s/g
var output = input.replace(regex, function($0, $1) {
return ($1 ? $0 : "-")
});
console.log(output);
https://stackoverflow.com/questions/46460497
复制相似问题