4

How would you go about doing a simultaneous regex replace in JS ES6?

Comments
  • 2
    @AlexDeLarge I guess he means a regex that finds what matches the expression and replace it by something else
  • 0
    @AlexDeLarge
    @deodexed
    Basically, if I've got the string 'foo foo bar', I want to do something like replace('foo foo bar', [[/foo/g, 'bar'], [/bar/g, 'foo' ] and it should return 'bar bar foo'
  • 1
    You'd have to evaluate all the occurrences of n conditions.
    I can see why you'd want to use it.
    Are you elaborating a proposal to a language of some sort of are you thinking about how to implement it?
    If the latter, I'd build a parser myself.
    Still, how you'd deal with conflicts between two different expressions?@ScriptCoded
  • 1
    Such as ['foo foo bar', [[/foo/g, 'fizz'], [/o\b/g, 'tacotuesday']]]?
  • 0
    @perotti I'm going to implement it in JS a some sort of function. The best solution I think would be to have whatever search matches first take precedence. Thing is, if the replacer is 'bar($1)', I think I'd like other matches to match the value of $1. Problem is that this is getting into some deep recursion, and I want it lightweight 😅
  • 0
    @ScriptCoded the problem with that logic is that your example would give us 'foo foo foo' in the end
  • 0
    @perotti Would it?

    /foo/g would match *foo foo* bar

    /bar/g would match foo foo *bar*

    So there are not conflicts

    What I'm proposing is that if two matches overlap, say /foo/ and /o f/, then the first one would take precedence.
  • 0
    @ScriptCoded if you are using tokens to represent change and expect no conflicts, ya. Else, something must take precedence
Add Comment