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

Consider supporting the full String.prototype.replace API #6

Open
iansinnott opened this issue Apr 28, 2016 · 8 comments
Open

Consider supporting the full String.prototype.replace API #6

iansinnott opened this issue Apr 28, 2016 · 8 comments

Comments

@iansinnott
Copy link
Owner

Not sure how big a lift this would be, but something to think about.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

@iansinnott
Copy link
Owner Author

Relates to #8

@pejrak
Copy link
Contributor

pejrak commented Nov 23, 2016

@iansinnott, I think, in order to have the API as below, the index would need to go and would introduce a breaking change to current react-string-replace API. I assume the index is only needed for key on multi component rendering and needs to be unique. So we could reuse the offset for that, which is unique. I am just still trying to understand what is the relationship between offset <> pN

function replacer(match, p1, p2, p3, offset, string) {
  // p1 is nondigits, p2 digits, and p3 non-alphanumerics
  return [p1, p2, p3].join(' - ');
}
var newString = 'abc12345#$*%'.replace(/([^\d]*)(\d*)([^\w]*)/, replacer);

@iansinnott
Copy link
Owner Author

Yeah, looking at the docs the string replace API seems a bit odd. I initially want with index for the reason you said, and simply because it's familiar.

Since offset is unique it does seem that we could make due without index. I like the idea of supporting the standard API but I also don't have a need for it. Open to discussion

@pejrak
Copy link
Contributor

pejrak commented Nov 29, 2016

@iansinnott, I think I grok it now. And I actually found a use-case in my application.

The use case is basically this:

  • in the provided text, I want to highlight the candidate strings (eg: words) for manipulation with one regex
  • in the same text, I need to highlight the manipulated strings (eg: fields in handlebar enclosures {{field.name}})

Now, I can provide a grouped regex for matching the two character groups eg: \{\{(.+?)\}\}|([^\s\n\.\,]{2,}), take the p1, p2 parts and return them with desired tags to the result array.

@pejrak
Copy link
Contributor

pejrak commented Nov 29, 2016

Let me give you a pull request.

@pejrak
Copy link
Contributor

pejrak commented Nov 29, 2016

@iansinnott I did not modify the server <> client example, since I do not understand how you deploy this on local... I suppose the code needs bundling first. There is no script in the package.json file, so babel-node does not load the app for me.

@pejrak
Copy link
Contributor

pejrak commented Nov 29, 2016

I have added a bit more information to readme, but I think I am making it a bit cluttered with former examples.

@erunion
Copy link

erunion commented Jun 25, 2018

Having support for this would be really great. I'm currently trying to transform /<h1( id="([a-z-]+)")?>(.*)<\/h1>/g into <Header1 key={i} id={match[1]}>match[1]</Header1>, but can't with this library.

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

No branches or pull requests

3 participants