[英]Need to remove whitespace from between two symbols
Need to remove whitespace from between two colons. 需要从两个冒号之间删除空格。 So the output of : test :
would be :test:
. 所以输出: test :
将:test:
. I got it to detect everything between :: but now I need it to match only the whitespace. 我得到它来检测之间的所有内容::但现在我需要它来匹配空格。 This is the regex that matches everything: (?<=\\:)(.*?)(?=\\:)
这是匹配所有内容的正则表达式: (?<=\\:)(.*?)(?=\\:)
Try capturing colon and replace()
: 尝试捕获冒号并replace()
:
/\s*(:)\s*/gi
var str = `fbflb : vfkvbfkvb : otjtjb : hih igvjyfv ugukgu ugug ` var rgx = /\\s*(:)\\s*/gi; var res = str.replace(rgx, '$1'); console.log(res);
Try this 尝试这个
": test :".replace(/: *(.*?) *:/g,":$1:")
let a = ": test :".replace(/: *(.*?) *:/g,":$1:") console.log(a);
If you wanna only remove spaces from string then use simply 如果你只想从字符串中删除空格,那么使用简单
": test :".replace(/ /g,'')
let a = ": test :".replace(/ /g,'') console.log(a);
Use Replace and capturing group 使用替换和捕获组
let str = ': test :' let op = str.replace(/(:)\\s+(.*?)\\s+(:)/g, "$1$2$3") let op1 = str.replace(/:(.*?):/g,':$1:' ) console.log(op); console.log(op1)
The following regex will remove all space characters from a string. 以下正则表达式将从字符串中删除所有空格字符。 \\s represents any "space" type of character including tabs. \\ s表示任何“空格”类型的字符,包括制表符。 the 'g' flag will perform multiple matches. 'g'标志将执行多个匹配。
let text = ': test :'
let textWithoutSpaces = text.replace(/\s/g, '');
// textWithoutSpaces === ':test:'
Use 采用
var s = " test : test : test"; console.log( s.replace(/:[^:]+:/g, function (m) { return m.replace(/\\s+/g, ''); }) )
Details 细节
/:[^:]+:/g
- matches all substrings that start with :
, then have 1+ chars other than :
and then :
/:[^:]+:/g
- 匹配所有以:
开头的子串,然后除了以下之外还有1个以上的字符:
然后:
m
is the matched text found, it is passed to the callback method in replace
method m
是找到的匹配文本,它被传递给replace
方法中的回调方法 m.replace(/\\s+/g, '')
removes all 1+ consecutive whitespace chars inside that match, and after this, the text is returned, and pasted back into the result. m.replace(/\\s+/g, '')
删除该匹配中的所有1个连续的空白字符,然后返回文本并粘贴回结果中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.