Skip to content

Instantly share code, notes, and snippets.

View irmmr's full-sized avatar

m.m irmmr

View GitHub Profile
@irmmr
irmmr / str_replace.js
Last active September 20, 2021 05:48
The "str_replace" function, same as php for javascript
/**
* Replace all occurrences of the search string with the replacement string.
* @param {array|string} search The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles.
* @param {array|string} replace The replacement value that replaces found search values. An array may be used to designate multiple replacements.
* @param {array|string} subject The string or array being searched and replaced on, otherwise known as the haystack. If subject is an array, then the search and replace is performed with every entry of subject, and the return value is an array as well.
* @param {object|null} count If passed, this will be set to the number of replacements performed.
* @returns {array|string} This function returns a string or an array with the replaced values.
*/
function str_replace(search, replace, subject, count = null) {
if ('' === subject) return ''