简体   繁体   English

匹配所有空格的正则表达式,除了?

[英]Regex that matches all spaces except?

Regex that matches all spaces but:匹配所有空格的正则表达式,但:

  1. \\sand\\s
  2. \\sor\\s
  3. \\sbut\\s
  4. ,\\s


Input输入

font weight, width and height, background, or background color, but transform style字体粗细、宽度和高度、背景或背景颜色,但变换样式


input.replace(regex,"-")


Matches火柴

font字体weight, width and height, background, or background重量、宽度和高度、背景或背景color, but transform颜色,但变换style风格


Replace代替

font - weight, width and height, background, or background - color, but transform - style字体-粗细、宽度和高度、背景或背景-颜色,但变换-样式


Output输出

font-weight, width and height, background, or background-color, but transform-style字体粗细、宽度和高度、背景或背景颜色,但变换样式

Since JavaScript doesn't support look behinds, you won't find a regex which will be able to do it as simply as the replace as you have there.由于 JavaScript 不支持后视,因此您将找不到像替换那样简单的正则表达式。 If you are willing to extend the replace logic a bit, you can use an answer such as the following (adapted from answer here: Regex negative lookbehind not valid in 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM