简体   繁体   English

正则表达式从字符串的开头和结尾删除某些字符

[英]Regex Expression to remove certain characters from string expect at the start and end

I'm trying to create a regex to remove all characters that are not space, tabs or alpha numeric from a string.我正在尝试创建一个正则表达式来从字符串中删除所有不是空格、制表符或字母数字的字符。 Though if the character % exists and the start or end of the string they should be not be matched.尽管如果字符 % 存在并且字符串的开头或结尾,则它们不应匹配。

So far i have this到目前为止我有这个

(?!<=^\%^)*[^\w\s](?<!%)

But it does not work as intented: https://regexr.com/5mckp但它没有按预期工作: https://regexr.com/5mckp

for example例如

%foo: bar [10]%
foo% bar%
foo%bar

should become应该成为

%foo bar 10%
foo bar%
foobar

Probably something like this should work:可能这样的事情应该有效:

(?!^\%*)[^\w\s](?<!\%$)

Your error stands in the fact that the sequence ^\%^ will never occur since you are asking that the start of the sequence is after a % sign.您的错误在于序列^\%^永远不会发生,因为您要求序列的开头在 % 符号之后。 Also the universal quantifier belongs to the % sign and not to the negative group.通用量词也属于%符号而不是负组。 You should also check for the end of the line $ .您还应该检查$行的结尾。 The rest was fine rest 很好

[^\w\s] does not match an underscore which is not alphanumeric, you need [^\w\s]|_ . [^\w\s]不匹配不是字母数字的下划线,您需要[^\w\s]|_

Capture % at the start and end to keep them, use在开始和结束时捕获%以保留它们,使用

string.replace(/(^%|%$)|[^\w\s]|_/g, '$1')

See proof .证明 No lookbehind ensures the pattern work in Safari and other browsers not supporting lookbehind yet.无后视可确保该模式在 Safari 和其他尚不支持后视的浏览器中工作。

JavaScript code : JavaScript 代码

 const strings = ['%foo: bar___ [10]%','foo% bar%','foo%bar']; strings.forEach( string => console.log(string.replace(/(^%|%$)|[^\w\s]|_/g, '$1')) )

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

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