正则表达式从javascript中删除字符串中的多个逗号和空格 [英] regex to remove multiple comma and spaces from string in javascript

查看:156
本文介绍了正则表达式从javascript中删除字符串中的多个逗号和空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串

var str=" , this,  is a ,,, test string , , to find regex,,in js.  , ";

其中字符串的开头,中间和结尾有多个逗号空格。我需要这个字符串

in which there are multiple spaces in beginning,middle and end of string with commas. i need this string in

var str="this is a test string to find regex in js.";

我发现很多正则表达式在论坛中删除空格,单独使用逗号,但我无法加入它们以删除它们。

i found many regex in forum removing spaces , commas separately but i could not join them to remove both.

请尽可能解释正则表达式syntex。

Please give explanation of regex syntex to if possible .

提前致谢

推荐答案

您可以用空格替换每个空格和逗号,然后修剪这些尾随空格:

You can just replace every space and comma with space then trim those trailing spaces:

var str=" , this,  is a ,,, test string , , to find regex,,in js.  , ";
res = str.replace(/[, ]+/g, " ").trim();

jsfiddle demo

这篇关于正则表达式从javascript中删除字符串中的多个逗号和空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆