javascript - Using string.prototype.replace with a regexp and a function -
the following returns 'entiy'. want return 'entity'. how can achieve this?
'entities '.replace(/\w(ies)(?:[\w|$|_])+/g, 'y');
just capture character before "ies":
'entities '.replace(/(\w)(ies)(?:[\w|$|_])+/g, '$1y');
now question asked using function; can too:
'entities '.replace(/(\w)(ies)(?:[\w|$|_])+/g, function(_, before, repl) { return before + "y"; });
i don't know want subsequent stuff after "ies"; can either capture , glue replacement, or else use positive look-ahead. portions of input text matched look-ahead not part of match involved replacement operation. in other words, look-ahead succeed or fail based on pattern, characters matched not made part of "to replaced" grouping.
Comments
Post a Comment