简体   繁体   English

如何从字符串的开头和结尾删除符号,除了某些符号?

[英]How to remove symbols from beginning and end of a string, except certain symbols?

I am currently using this regex: 我目前正在使用这个正则表达式:

/^[^a-z\d]*|[^a-z\d]*$/gi

This checks for a symbol in the beginning and end of a string. 这将检查字符串开头和结尾的符号。

However, I want to exclude these 4 symbols from being removed: 但是,我想排除这4个符号被删除:

(
)
[
]

How can I modify my regex so that it doesn't remove those symbols? 如何修改我的正则表达式,以便它不会删除这些符号?

Then you can add them to the excludes list: 然后,您可以将它们添加到排除列表中:

/^[^\(\[\)\]a-z\d]*|[^\(\[\)\]a-z\d]*$/gi

(if I understand correctly and you want to remove all symbols up to the first occurence of one of these symbols - if not, this will not be a single regex, I think) (如果我理解正确并且您希望删除所有符号,直到第一次出现这些符号之一 - 如果没有,这将不是一个正则表达式,我认为)

Here is Fiddle 这是小提琴

It can remove all the special characters include ()[] 它可以删除所有特殊字符include ()[]

var test = "([xxxx † yyyy § zzzz)]";
alert(test.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ''));

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

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