简体   繁体   English

RegExp用于从括号侧面移除空白区域

[英]RegExp for removing white space from sides of brackets

I am stuck at creating a pattern to removes all white space after [ and before ] 我被困在创建一个模式以删除[和之前]之后的所有空格

replacement should produce: 替换应产生:

--> [  tag content ] ---> [tag content]

Can somebody please help me write this regular expression 有人可以帮我写这个正则表达式吗

You could: 你可以:

str = str.replace(/\[\s*(.*?)\s*]/g, '[$1]');

or 要么

str = str.replace(/\[\s*/g, '[').replace(/\s*]/g, ']');

这应该可以帮助您:

str = str.replace(/(\[)\s+|\s+(])/g, '$1$2');

也试试这个:

a.replace(/\s*(\w+\s+\w+)\s*/g,"$1");

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

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