Show HN: A JavaScript String replace API that challenges the status quo
github.com
github.com
function replace(text, {fromText = '', toText = '', caseInsensitive = false} = {}){
//
// ...
//
}
replace(
'Get on the Road',
{
fromText: 'road',
toText: 'bus',
caseInsensitive: true
}
);const target = 'Get on the Road' const result = target.replace(/road/i, 'bus')
str-replace uses regex internally but abstracts it in a fluent interface for legibility purposes at the cost of not optimizing prematurely. Regex is definitely the solution if one want to make more complex replaces or need to care about micro-optimizations like the use of regex without an additional function call (which imho in 90% of the cases is irrelevant).
Nonetheless, I commend you for putting a well documented/examples API out there. Cheers.
I think it would be useful if there was a very good reason to use it. Otherwise, learn regexing and it shouldn't be so bad.