简体   繁体   English

仅从字符串的开头和结尾替换或删除字符串中的某些特殊字符,而不是从javascript的字符串中间

[英]replace or remove some special characters in string only from starting and ending , not from in between of string in javascript

replace or remove some special characters (-) in string only from starting and ending , not from in between of string in javascript. 仅从字符串的开头和结尾替换或删除字符串中的某些特殊字符(-),而不是从javascript的字符串中间删除或删除它们。

the string is fully dynamic , count of special characters (-) at starting and ending will be different every time , and the string will also change every time , so we have to develop dynamic logic. 字符串是完全动态的,开始和结束时特殊字符(-)的数量每次都会不同,并且字符串也会每次更改,因此我们必须开发动态逻辑。

ex. 例如

original string = "-----hello--stack--over--flowj-----"
output string = "hello--stack--over--flowj"

I have tried replace and substring methods , but i was unable to remove specifically starting and ending special character (-) , and for that I was thinking for loops and to count special characters (-) and that all. 我尝试了replace和substring方法,但是无法删除特定的开始和结束特殊字符(-),为此,我正在考虑循环并计算特殊字符(-)等等。

Thank You. 谢谢。

您正在寻找replace()函数:

string.replace(/^-*|-*$/g,"")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM