我有一个Javascript字符串:
var myString= "word = another : more new: one = two";
我正在试图弄清楚会产生这种情况的正则表达式:
var myString= "word another more new: one two";
因此,当a =符号后跟另一个空格的空格模式会导致=符号被删除。
同样适用于:角色。
如果删除了=字符或:字符,那么这些字符也可以用空格字符替换。
总之,要替换多次出现的=或a:当且仅当它们 被空间角色包围。
无论哪个正则表达式都更容易编写。
答案 0 :(得分:1)
不是用javascript ...但你明白了:
echo "word = another : more new: one = two" | sed 's/ [:=] / /g'
返回所需的字符串:
word another more new: one two
说明:表达式/ [:=] /
找到所有"空格后跟冒号或等号后跟空格"并替换为" space"。
答案 1 :(得分:0)
//save the appropriate RegEx in the variable re
//It looks for a space followed by either a colon or equals sign
// followed by another space
let re = /(\s(=|:)\s)/g;
//load test string into variable string
let string = "word = another : more new: one = two";
//parse the string and replace any matches with a space
let parsed_string = string.replace(re, " ");
//show result in the DOM
document.body.textContent = string + " => " + parsed_string;