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: