[英]Regex that matches all spaces except?
匹配所有空格的正则表达式,但:
\\sand\\s
\\sor\\s
\\sbut\\s
,\\s
输入
字体粗细、宽度和高度、背景或背景颜色,但变换样式
input.replace(regex,"-")
火柴
字体重量、宽度和高度、背景或背景
颜色,但变换
风格
代替
字体-
粗细、宽度和高度、背景或背景-
颜色,但变换-
样式
输出
字体粗细、宽度和高度、背景或背景颜色,但变换样式
由于 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.