[英]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.