-5

That is, is there a way to find which parts of a string match two or more regular expressions such as string.replace(/foo/g, /bar/g, "baz");?

EDIT: No, I am not looking for a single regular expression to match the two. A more appropriate example would be string.replace(foo, /bar/g, "baz").

EDIT 2: Sorry, I am a new user. An example input would be "abc".replace(x, /[bc]/g, "[match]"), where x is a variable with the value /[ab]/g, which in that case should return a[match]c.

BobTheAwesome
  • 131
  • 12
  • 1
    And what should by replaced with what? – Oriol Feb 16 '15 at 17:19
  • 2
    Please elaborate, I do not comprehend what you are asking –  Feb 16 '15 at 17:21
  • @BobTheAwesome for you what's the difference between `string.replace(/foo/g, /bar/g, "baz");` and `string.replace(foo, /bar/g, "baz")` ? just do `string.replace(/(foo|bar)/g, "baz");` – Miguel Mota Feb 16 '15 at 17:22
  • What is `string.replace(foo, /bar/g, "baz").` meant to mean? Usually, your best bet here is to show inputs and desired outputs. – T.J. Crowder Feb 16 '15 at 17:22
  • `string.replace( regexp, string )` -- Does not take a variable amount of arguments. –  Feb 16 '15 at 17:23
  • @Moogs **And**. I am looking for the parts of a string that match **two** regular expressions. That is, match **both** of them. – BobTheAwesome Feb 16 '15 at 17:28
  • @T.J.Crowder Sorry, I added that. – BobTheAwesome Feb 16 '15 at 17:29
  • 1
    I think it's a good question, it could just use some more clarification. I think most of us are confused. –  Feb 16 '15 at 17:29
  • @Isaiah I know that. I couldn't think of a better example syntax. – BobTheAwesome Feb 16 '15 at 17:29
  • Flagging as duplicate; see 2nd comment on the answer, as that I feel best describes what you are trying to accomplish and why this wont work.... Well, I would except I already flagged it as unclear earlier.... –  Feb 16 '15 at 17:34
  • This is not a duplicate of that question. I am asking for an "and" operator for regular expressions in javascript. They are asking specifically about HTML. My question has no HTML in it, neither is it tagged "HTML". – BobTheAwesome Feb 16 '15 at 17:36
  • @BobTheAwesome like this ? https://regex101.com/r/xJ5oI4/2 – Miguel Mota Feb 16 '15 at 17:37
  • Ugh, I raise a mod attention flag (for duplicate) then someone posts what looks like the perfect answer for your question……… oh well........ –  Feb 16 '15 at 17:40

3 Answers3

1

Given your example, I think you mean "OR", not "AND". The | operator can be used for this:

string.replace(/(foo|bar)/g, "baz");
radiaph
  • 4,001
  • 1
  • 16
  • 19
  • I mean and. For example, matching the characters in the HTML of the document body that are A's but are also not in HTML tags (e.g. "span"). – BobTheAwesome Feb 16 '15 at 17:22
  • 2
    @BobTheAwesome: [Don't use regex to parse HTML](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). Use a parser to parse HTML. – T.J. Crowder Feb 16 '15 at 17:23
  • @T.J.Crowder I am creating a script to color-code characters in the documentation for the yet-to-be-named [two-dimensional programming language](https://www.khanacademy.org/computer-programming/two-dimensional-programming-language/6252487852687360), an esoteric programming language I coded in the KACS Environment. The script will go through the characters and surround, for example, characters that regulate program flow in a ``. – BobTheAwesome Feb 16 '15 at 17:34
  • 1
    @BobTheAwesome: Which means you'll have to handle keywords that are within literal strings differently from keywords that aren't, and presumably the language allows escaping the quotes around strings. Regex isn't the right tool for that job, sadly. Now, if you *didn't* need to handle not doing it within literal strings, that would be different... – T.J. Crowder Feb 16 '15 at 17:40
  • @BobTheAwesome: And btw, that sounds nothing like what you've described in your question. – T.J. Crowder Feb 16 '15 at 17:41
1

An example input would be "abc".replace(x, /[bc]/g, "[match]"), where x is a variable with the value /[ab]/g, which in that case should return a[match]b.

Sounds like you're looking for capture groups and a replace callback.

  • A capture group is a part of the regular expression telling the engine to "capture" the bit that matched that part of it.

  • A replace callback is a function that replace will call for each match, letting you create the replacement text. The function is passed the overall match and any capture groups.

So for instance, if you wanted to match anything between the letters a-z and the numbers 0-9, and do something to the bit in-between:

var result = "afoo9".replace(/([a-z])(.+)([0-9])/g, function(match, c0, c1, c2) {
    return c0 + c1.toUpperCase() + c2;
});
  • ([a-z]) means "match any character from a to z and capture it"

  • (.+) means "match one or more characters here and capture them"

  • ([0-9]) means "match any character from 0 to 9 and capture it" (could also be written (\d))

That's quite contrived, of course, and you probably want to match more than one character at each end, but that's a regex detail.

Live example:

var str = "afoo9";
var result = str.replace(/([a-z])(.+)([0-9])/g, function(match, c0, c1, c2) {
    return c0 + c1.toUpperCase() + c2;
});
snippet.log("String: " + str);
snippet.log("Result: " + result);
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I know what both of these are, I am asking for an "and" operator! What parts of a string match two regular expressions? This is not an answer to my question! – BobTheAwesome Feb 16 '15 at 17:39
  • 1
    @BobTheAwesome: No need to shout. If this isn't answering your question, then your question remains ***really*** unclear. – T.J. Crowder Feb 16 '15 at 17:40
  • 1
    @BobTheAwesome, there is no such operator. T.J. Crowder is showing you the closest approximation. – radiaph Feb 16 '15 at 17:41
0

You could first extract with the first Regex the strings that fulfill the first rule and than use a second regex (or replace with regex) to do the action.

Sheki
  • 1,597
  • 1
  • 14
  • 25