Giter VIP home page Giter VIP logo

Comments (8)

iansinnott avatar iansinnott commented on July 17, 2024

Hm, couldn't you do this with regex? I.e. something like /(whats|your)/g.

reactStringReplace('whats your name', /(whats|your)/g, (match, i) => (
  <span>{match}</span>
));

from react-string-replace.

Floriferous avatar Floriferous commented on July 17, 2024

Update: This argument is wrong, it is totally possible to do this without using an array. I guess it comes down to convenience in my case.

Yes that would totally be possible, however, this prevents you from using the match argument to style, or add props to the replacing element. For example:

reactStringReplace('whats your name', ['whats', 'your'], (match, i) => (
  <span className={`${match}`}>{match}</span>
));

or more generalized:

reactStringReplace('whats your name', ['whats', 'your'], (match, i) => (
  <span props={someFunc(match)}>{match}</span>
));

This makes it much more powerful.

from react-string-replace.

iansinnott avatar iansinnott commented on July 17, 2024

In either case, match should still just be a string. How does using a regex prevent you from using the match to style?

from react-string-replace.

Floriferous avatar Floriferous commented on July 17, 2024

You're absolutely correct, it seems like the only advantage is to make the public API more friendly to those who aren't well versed in regular expressions (like me obviously :)).

(updated my comment above)

from react-string-replace.

iansinnott avatar iansinnott commented on July 17, 2024

Yup, that's a fair point. Regex is not a very friendly API to anyone :) I agree this might make it easer to use for some people, but I'm hesitant to add functionality that is not part of the standard String.prototype.replace API. It adds more complexity by be different than JS replace. See #6.

Maybe a readme example would help people learn how to use regex to match more than one string?

from react-string-replace.

Floriferous avatar Floriferous commented on July 17, 2024

Okay, so it seems that this is a way to turn an array of strings into a single regex:

new RegExp(`(${myArray.join('|')})`, 'gi')

Do you know of a cleaner way? Also, would this work with an array of regexes ?

Do you think it makes more sense to add this to the ReadMe, or could we add a built in one liner that transforms arrays into a regex?

from react-string-replace.

iansinnott avatar iansinnott commented on July 17, 2024

Yup, your right that you can turn a string into a regex like that. It works great but that's not why I'm reluctant to add the functionality to this library. Adding a new argument type makes the API more complex so the user has to be aware of the type of the argument in order to understand how the function will work. This is already the case with String vs RegExp, so adding a third type (Array) would mean more complexity for users. Also more code in the codebase means more places things can break.

But I understand that it might be much more useful to you if it supported arrays, so in that case I would recommend you wrap it in your own function that creates a regex from an array and passes it through:

// myReactStringReplace.js
import reactStringReplace from 'react-string-replace';

export default function myReactStringReplace(str, regex, replacer) {
  if (Array.isArray(regex)) regex = new RegExp(`(${regex.join('|')})`, 'gi');
  return reactStringReplace(str, regex, replacer);
}

from react-string-replace.

Floriferous avatar Floriferous commented on July 17, 2024

Perfect, solves the issue!

from react-string-replace.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.