删除逗号和任何字母之间的空格
Remove white spaces from string between comma and any letter
我使用RegExp和"string"。很少匹配,所以我不太确定如何在一些复杂的事情上使用它们。这是我想做但不知道怎么做的事情。这里我有一个javascript字符串
var str= " I would like to know how to use RegExp , string.match and string.replace"
我想删除逗号和任何字母之间的所有空白。在那之后,这个字符串看起来是这样的
str= " I would like to know how to use RegExp,string.match and string.replace"
我只知道如何从字符串中删除所有空白使用这个->
str = str.replace(/'s/g, "")
应该可以了:
str = str.replace(/'s*,'s*/g, ",");
var str = " I would like to know how to use RegExp , string.match and string.replace";
console.log(
str
);
console.log(
str
//Replace double space with single
.replace(/ +/ig, ' ')
);
console.log(
str
//Replace double space with single
.replace(/ +/ig, ' ')
//Replace any amount of whitespace before or after a `,` to nothing
.replace(/'s*,'s*/ig, ',')
);
使用RegEx:
's*,'s*
演示您可以尝试使用正则表达式,并在https://regex101.com
上获得有关该语言特性的体面文档。这是这个。lau_的解决方案:https://regex101.com/r/aT7pS5/1
我甚至会建议一个更好的包括引号:
const text: string = 'Example "number-1" , something “ABC” , and something more.';
const regex: RegExp = /'s(?=[,"”])/g;
const resultat: string = text.replace(regex, '');
Example "number-1", something “ABC”, and something more.
相关文章:
- JS中单词之间的空格
- 在php和javascript onClick之间传递带有空格的值
- 如何在城市和州代码之间用逗号将城市和州从短划线格式化为空格.(javascript)
- 突出显示jQuery中单词之间的空格
- 使用 JavaScript 删除标签之间的每个空格
- 正则表达式和 javascript:匹配数字后跟空格和大写字母之间的所有内容
- 行之间有空格会影响 JavaScript 代码的性能吗?
- 在某些情况下,“text”方法会删除单词之间的空格
- 使用正则表达式获取多个单词,单词之间留有空格
- 通过JQuery在网页上的两个字符之间放置空格
- 空格上的正则表达式匹配 - 排除多组字符之间的空格
- jquery 移动控件组之间的空格
- 当数据库值为 NULL 时,如何在 XML 标记之间插入空格
- 如何删除句点和前一个单词之间的空格
- 删除带有单词javascript的大括号之间的空格
- 什么是用于删除大写字母之间的空格的正则表达式,但在单词之间保留空格
- 删除行之间的空格
- JS 正则表达式以匹配字符串,令牌之间最多有 1 个空格
- 文本之间的空格,但文本前后不空格
- 如何使用 javascript 删除 html 标记之间的空格