Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #14766

Re: reg expression

Newsgroups comp.lang.javascript
From Spamless <Spamless@Nil.nil>
Subject Re: reg expression
References <jsfv2r$sks$1@speranza.aioe.org> <370nu797jqo96n2cai31am4j64ac7j0uqv@hive.bjoern.hoehrmann.de> <H6FOnrFD2J7PFwND@invalid.uk.co.demon.merlyn.invalid> <c4udnXrzrbCwMXDSnZ2dnUVZ_tKdnZ2d@inch.com> <jske1l$c6k$1@speranza.aioe.org>
Message-ID <-OydnWwzV_IYRHDSnZ2dnUVZ_oSdnZ2d@inch.com> (permalink)
Date 2012-06-29 11:36 -0500

Show all headers | View raw


On 2012-06-29, Phil Hunt <aaa@aaa.com> wrote:
> My requirement deos not care how many times or where the string may occur.

Well, if you want to find out if both "one" and "two" are in the string
and you can find the number of matches of different content to 'one|two'
(different content - not the same content at different locations) then you
do want to know that number - or at least that it is two (if it's two you
matched both, otherwise you matched one or none.)

> I have another question. When the patern contains '(' it get an exception.
> How do I escape that and other character that may give me trouble.

I remember trying to do a string.replace(/./g,...) on a string with embedded
end of line characters and /./ does not match those. So ... I use /\x00-\xff/
(they were all single byte characters). That doesn't help here, though,
since a /...(.../ will take "(" as marking the beginning of a group. In that
case you need to escape "(" (and ")"). That is a little tricky.

  re = /\)/;
  ")".match(re)
matches but ...

  string="\)";
  re=/string/;
  ")".match(re)
does not match.

Why? This is literal syntax and re=/string/ matches
the string "string".

  re = RegExp(")");
tries to create a regular expression with a group closer
(")") and fails.

  re = RegExp("\)") also fails (the string "\)" is, internally,
                                 still the string ")")

When you set string="\)" the internally stored string
is the escaped value of ")". In Javascript everything
has an escaped value and if a separate/different value
is not specified, then the escaped value is the same as
the normal value (this is different from, say, the way
PHP handles escapes). In Javascript, "\Q" is (internally)
just (the same as) "Q". If Q is not escapable in PHP then
"\Q" is (internally) "\Q" (it does not lose the backslash).
If you really want "\Q" in a string, you can use "\\Q"
or even (since, in Javascript \Q and Q are the same),
"\\\Q"!

We want "\)" internally (the string to be used in the
regex) so we use "\\)" (or even "\\\)"!).

  string="\\)";
  re=RegExp(string);
  ")".match(re)

(even string="\\\)";
      re=RegExp(string);
 works)
  matches.

Here, '\\)' is internally '\)' (so is '\\\)')
while "\)" is just ")" internally.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

reg expression "phil hunt" <a@abc.com> - 2012-06-27 17:48 -0400
  Re: reg expression Bjoern Hoehrmann <bjoern@hoehrmann.de> - 2012-06-27 23:55 +0200
    Re: reg expression Dr J R Stockton <reply1226@merlyn.demon.co.uk.not.invalid> - 2012-06-28 19:08 +0100
      Re: reg expression Spamless <Spamless@Nil.nil> - 2012-06-29 08:22 -0500
        Re: reg expression "Phil Hunt" <aaa@aaa.com> - 2012-06-29 10:19 -0400
          Re: reg expression Spamless <Spamless@Nil.nil> - 2012-06-29 11:36 -0500
          Re: reg expression Gene Wirchenko <genew@ocis.net> - 2012-06-29 10:05 -0700
  Re: reg expression Gene Wirchenko <genew@ocis.net> - 2012-06-27 19:38 -0700
    Re: reg expression Gene Wirchenko <genew@ocis.net> - 2012-06-27 21:18 -0700
    Re: reg expression "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-06-28 06:24 +0000
      Re: reg expression Spamless <Spamless@Nil.nil> - 2012-06-28 05:36 -0500
        Re: reg expression "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-06-28 15:15 +0000
          Re: reg expression "Phil Hunt" <aaa@aaa.com> - 2012-06-28 12:08 -0400

csiph-web