Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace all occurrences of substr in str by newstr #359

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tshemsedinov
Copy link
Member

  • code is properly formatted (npm run fmt)
  • tests are added/updated
  • documentation is updated (npm run doc to regenerate documentation based on comments)
  • description of changes is added under the Unreleased header in CHANGELOG.md

Usage: `replace(str, substr, newstr)`
Copy link
Member

@belochub belochub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tshemsedinov Is this basically a simpler polyfill for String#replaceAll() without the support for regular expression and function arguments?

@tshemsedinov
Copy link
Member Author

Something like that, just replace all occurrences (like replace global flag) of sub-string in the given string with another sub-string. @belochub

//
// Example: replace('a20w10z2a22aa0', 'a2', 'z') = 'z0w10z2z2aa0'
const replace = (str, substr, newstr) => {
if (substr === '') return str;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mention: it works different than String.prototype.replaceAll:

replace('a2', '', 'z')      // 'a2'
'a2'.replaceAll('', 'z')    // 'zaz2z'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants