I have a whole array of regexp queries to make recursively going through the string in order.
Is there a way I can do it all with 1 call such as (doesn’t work)?
str.replace(/query1|query2|query3|query4|...|[0-9]+|[^]/,
"reslt1|reslt2|reslt3|reslt4|...|procNumb|procChar");
It need only work in Firefox. Right now I’m stuck with:
str.replace(... ,function(a,b,c){if (a=="query1") return "reslt1"; ..............});
Thanks!
Edit: Sorry if confusing. Goal:
Before: “query1query3x123query2”
After: “reslt1reslt3procChar(x)procNumb(123)reslt2”
The main thing is I need I to process the string 1 fragment at a time recursively, so I think I must use a super-query like that to match any, or not use regexes at all. I just wonder if there’s a way to automatically pair the results to the queries when using lots of pipes. I am not so familiar with javascript regex but I couldn’t find anything on mdc unfortunately.
If you’re trying to match any of several alternative substrings and provide a different result for each match, you’re going to have to use a function (as you appear to be doing). For instance:
(Of course, that particular example would be better done as an array lookup.)
If you’re a vertical brevity fiend (I’m not, but some are), you can use early returns:
Edit Just to be clear, about my “array lookup” comment above: If the replacements really are just a static lookup, you can do this:
…since JavaScript objects are associative arrays.