Module: js-whatever/js/regex-replace

js-whatever/js/regex-replace

require("js-whatever/js/regex-replace")(regex, text, yesFn, noFn) → {string}

Version of 'string'.replace(regex, function(){}) which will apply noFn to the parts of the string that
don't match and yesFn to the parts which do match

Parameters:
Name Type Description
regex RegExp
text string

Text to perform replacements on

yesFn function

Function applied to parts of the string which match regex

noFn function

Function applied to parts of the string which do not match regex

Source:
Returns:

text after the application of the appropriate functions

Type
string
Example
var wrapper = function(match) {
    return '{' + match + '}'
}

regexReplace(/\d/g, '12345abc', wrapper, _.identity); // returns '{1}{2}{3}{4}{5}abc'
regexReplace(/\d+/g, '12345abc', wrapper, _.identity); // returns '{12345}abc'
regexReplace(/\d+/g, '12345abc678', wrapper, _.identity); // returns '{12345}abc{678}'
regexReplace(/\d+/g, '12345abc', _.identity, wrapper); // returns '12345{abc}'
Source:
Copyright 2013-2018 Micro Focus International plc. Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
Documentation generated by JSDoc 3.4.3 on Thu Jun 21st 18:04 2018 using the DocStrap template.