javascript - 字符串中的通配符替换

标签 javascript jquery string

例如,我有这个脚本:

var text = '09';

$( 'p.container' ).html( function ( i, html ) {
    var regexp, replacement;

    regexp = RegExp( '(' + text + ')', 'gi' );
    replacement = '<span class="highlight">$1</span>';

    return html.replace( regexp, replacement );
});

容器包含以下值:

  • 023209
  • 232332
  • 092323
  • 837209

现在我只想突出显示以09结尾的值(请注意第三个值以09开头),我该如何比较它?类似的东西

html.replace( "****09", replacement );

编辑:

容器包含以下值:

  • 023209
  • 232308
  • 092323
  • 837209

现在我只想突出显示每个值的最后 2 个字符中第一个字符 = 0 的值

最佳答案

尝试像这样的正则表达式

var text = '09';

var regexp = RegExp('(\d*' + text + ')(\\s|$)', 'gi'),
  replacement = '<span class="highlight">$1</span>$2';
$('p.container').html(function(i, html) {
  return html.replace(regexp, replacement);
});
.highlight {
  color: red
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p class="container">023209 232332 092323 837209</p>

关于javascript - 字符串中的通配符替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26771818/

相关文章:

javascript - window.onload 无法正常工作

javascript - 表单提交时 setTimeout 返回 'not a function' 错误

jquery - 如何在 jQuery 中销毁 $.animate 函数?

javascript - 使用Ajax进行页面更改,仍然保存输入的数据

c# - 按条件并保持顺序将两个字符串合并为一个字符串

java - 提取 : and WORD in java using regex in java 之间的子字符串

javascript - 将嵌套值添加到关联数组 - JS

jquery 选择器和单击事件不起作用

python - Pandas 使用 regex=true 替换部分单词

javascript - ES6 生成器 : why the argument passed to the first next() function doesn't work?