Javascript正则表达式匹配和替换

时间:2019-03-08 15:31:18

标签: javascript jquery regex

我目前有这种正则表达式替换功能

SELECT  TOP 100  "public".ids.ids
FROM "public".basic
JOIN "public".ids ON "public".basic.oid = "public".ids.oidref
WHERE      "public".basic.main_id = 'm13' 

例如

var regex = new RegExp(value, 'gi');

var return = item.replace(regex, function(match) { return "<strong>" + match + "</strong>" });

其中

value = 'a';

它返回='C a t狗 A pple';

我想要的

  • 仅在单词开头匹配
  • 匹配不区分大小写的
  • 使整个单词更坚固,不仅是字母

所以结果应该是

item = 'Cat Dog Apple';

1 个答案:

答案 0 :(得分:2)

您正在寻找

\ba\w+

请参见a demo on regex101.com


JavaScript中:
let items = ['Cat', 'Dog', 'Apple', 'advertisement', 'New York'];
let regex = /\ba\w+/gi;

items.forEach(function(item) {
    let new_item = item.replace(regex, function(match) {
        return "<strong>" + match + "</strong>";
    });
    console.log(new_item);
});