![](/img/trans.png)
[英]need to remove special characters in the midle of sentence but not starting and ending of the string
[英]replace or remove some special characters in string only from starting and ending , not from in between of string in javascript
仅从字符串的开头和结尾替换或删除字符串中的某些特殊字符(-),而不是从javascript的字符串中间删除或删除它们。
字符串是完全动态的,开始和结束时特殊字符(-)的数量每次都会不同,并且字符串也会每次更改,因此我们必须开发动态逻辑。
例如
original string = "-----hello--stack--over--flowj-----"
output string = "hello--stack--over--flowj"
我尝试了replace和substring方法,但是无法删除特定的开始和结束特殊字符(-),为此,我正在考虑循环并计算特殊字符(-)等等。
谢谢。
您正在寻找replace()
函数:
string.replace(/^-*|-*$/g,"")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.