w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
JavaScript regex match doesn't return the full match

You just need to escape the backslashes since you're creating the regex from the new RegExp construct:

var content = 'This is my content which contains KEYWORD and
should return seven words. ';
var regex = new RegExp('(?:(?:\w+\W*){0,3})' + 'keyword' +
'(?:(?:\W*\w+){0,3})', 'gi');

var excerpts = regex.exec(content);
alert(excerpts[0]);

jsfiddle demo





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.