在逗号之前删除空格

时间:2015-05-13 07:47:35

标签: jquery html regex

我有来自DB的以下输出:

<span data-title="Score">Score: 6-3 , 6-4</span>

我在逗号之前有额外的空间,我需要摆脱它。 我正在使用jQuery实现这一点,因为我自己无法访问数据库。 如果有人能为我提供适当的RegEx,那么我可以节省一些时间。

谢谢

3 个答案:

答案 0 :(得分:6)

您需要使用:

$('[data-title="Score"]').text().replace(/ \,/g, ','); //or .replace(" ,", ','); for single occurence

如果你想替换文字:

$('[data-title="Score"]').text(function( index,text ) {
  return text.replace(/ \,/g, ','); //or .replace(" ,", ','); for single occurence
});

<强> Working Demo

答案 1 :(得分:0)

您可以使用replace()功能:

$('[data-title="Score"]').text().replace(' ,', ',');

您可以找到有关replace here

的更多信息

答案 2 :(得分:0)

试试这个:

$(function(){
var textWithoutcomma = $('span').text().replace(' ,',',');
console.log(textWithoutcomma);
});
相关问题