[英]how to remove just one character in a string of javascript
#a:{width:100px;height:100px;background-color:black;}#b:{width:100px;}
i have the above string 我有上面的字符串
i want that the character :
only after css selector like #a
and #b
get removed from this string 我想要那个角色
:
只有在#a
和#b
等css选择器从这个字符串中删除之后
i thought that i must use regular expressions so i wrote one: 我认为我必须使用正则表达式,所以我写了一个:
/[#\.A-Za-z0-9]+([:])[{]/g
see this regular expression working on regex101 看到这个正则表达式在regex101上工作
but you know it matches :
but when i try to remove this using replace
method then whole #a:{
and #b:{
get removed 但是你知道它匹配
:
但是当我尝试使用replace
方法删除它时,整个#a:{
和#b:{
被删除
any help would be great! 任何帮助都会很棒!
The regex is almost correct. 正则表达式几乎是正确的。 What you need to do is to repalce the with
$1$2
instead of null string 你需要做的是用
$1$2
而不是null字符串重新生成
Also make a small change to the regex as 同样对正则表达式进行一些小改动
/([#.A-Za-z0-9]+):({)/g
Changes made 做出了改变
([#.A-Za-z0-9]+)
enclosed in brackets. ([#.A-Za-z0-9]+)
括在括号内。 The matched string is captured in $1
hence for the frist match $1
will contain #a
匹配的字符串在
$1
捕获,因此第一个匹配$1
将包含#a
.
.
as it looses it meaning in the class. [{]
to ({)
The []
surrounding does not make any difference, hence drop it. [{]
to ({)
[]
周围没有任何区别,因此放弃它。 Enclosed in ()
, hence captured in $2
, for example in first match the $2
will contian {
附在
()
,因此在$2
被捕获,例如在第一场比赛中, $2
将成为contian {
Replace string $1$2
替换字符串
$1$2
will give output as 将输出作为
#a{width:100px;height:100px;background-color:black;} #b{width:100px;}
Javascript 使用Javascript
var value = "#a:{width:100px;height:100px;background-color:black;}#b:{width:100px;}";
alert(value.replace(/(#.):/g, "$1"));
Example: http://jsfiddle.net/7hs0jgd2/ 示例: http : //jsfiddle.net/7hs0jgd2/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.