[英]How replace Space ONLY before and after Character not before and after number
all i tryed and found, will remove all spaces. 我尝试并发现的所有内容都将删除所有空格。 But thats not what i want
但这不是我想要的
my string is 我的绳子是
var str = "24 july 2014 12 5";
i want 我想要
"24july2014 12 5"
i found this code example 我找到了这个代码示例
var str = "Paul m'a dit « Bonjour ! »";
str = str.replace(/\s([!:\?…;»])/g, function (el1, el2) {
return ' ' + el2;
}).replace(/(«)\s/g, function (el1, el2) {
return el2 + ' ';
});
console.log(str);
but its not changeable to my problem. 但它对我的问题没有改变。 maybe i`am too much noob?
也许我太多了菜鸟?
You can do this. 你可以这样做。
var str = "24 july 2014 12 5"; str = str.replace(/ (?=[az])/g, "").replace(/([az]) /g, "$1"); console.log(str);
JS doesn't support lookbehinds, so one workaround is to use capturing groups and echo. JS不支持回溯,因此一种解决方法是使用捕获组和回显。 The other replace is supported by a lookahead.
前瞻支持另一个替换。
OP, based on your comment, it seems like you also wish to decode URL params. OP,根据您的评论,您似乎还希望解码URL参数。 This is the right way to do it:
这是正确的方法:
string = "alabama%20justin"; console.log(decodeURIComponent(string));
Probably a more fancy way but simple check to see if it is a space followed by alpha or an alpha followed by a space 可能是一种更理想的方法,但只需简单检查一下它是一个空格,然后是alpha还是一个alpha后面是一个空格
"12 july 2014 12 5".replace(/\s+([a-z])|([a-z])\s+/ig,'$1$2')
or 要么
"12 july 2014 12 5".replace(/\s+([\D])|([\D])\s+/ig,'$1$2')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.