Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #14763
| From | "Phil Hunt" <aaa@aaa.com> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: reg expression |
| Date | 2012-06-29 10:19 -0400 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <jske1l$c6k$1@speranza.aioe.org> (permalink) |
| References | <jsfv2r$sks$1@speranza.aioe.org> <370nu797jqo96n2cai31am4j64ac7j0uqv@hive.bjoern.hoehrmann.de> <H6FOnrFD2J7PFwND@invalid.uk.co.demon.merlyn.invalid> <c4udnXrzrbCwMXDSnZ2dnUVZ_tKdnZ2d@inch.com> |
My requirement deos not care how many times or where the string may occurs.
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.
Thanks
"Spamless" <Spamless@Nil.nil> wrote in message
news:c4udnXrzrbCwMXDSnZ2dnUVZ_tKdnZ2d@inch.com...
> On 2012-06-28, Dr J R Stockton <reply1226@merlyn.demon.co.uk.not.invalid>
> wrote:
>> In comp.lang.javascript message <370nu797jqo96n2cai31am4j64ac7j0uqv@hive
>> .bjoern.hoehrmann.de>, Wed, 27 Jun 2012 23:55:25, Bjoern Hoehrmann
>><bjoern@hoehrmann.de> posted:
>>
>>>* phil hunt wrote in comp.lang.javascript:
>>>>In Jsctipt, I have
>>>>
>>>>re = new RegExp("one|two", "gi");
>>>>
>>>>"one three".search(re); return an integer >-1, meaning it found a match
>>>>
>>>>My question is how do I set up the RegExp to match "one" AND "two". I
>>>>though
>>>>an & would do it, but no.
>>>
>>>JavaScript regular expressions do not support intersections, you would
>>>have to use several regular expressions (or compute the intersection in
>>>some other tool).
>>
>> Not necessarily. re = new RegExp("one.*two|two.*one", "gi");
>>
>> But should "oneight" be considered to contain both "one" & "eight" ?
>>
>
> How about this? I am assuming these are disjoint strings
> (as someone noted - do you want to see a match of "one" and
> "eleven" in the string "oneleven"?) or at least can only appear
> disjointly.
>
> Do a global match on the alternation/disjunction of the
> strings, 'one|two" and see how many (distinct) matches
> you get
> str.match(/one|two/g).length
> will give the number of matches (if it's two, you matched
> two times - but maybe you matched "one" twice!).
>
> If you want to count the number of distinct matches ... well,
> it is easier to count the number of distinct elements of an
> array if it is sorted, so ...
>
> LOOKING FOR "one" and/or "two" IN A STRING, str:
> ------------------------------------------------
> str="onetwothreetwoone";
>
> old="";count=0;
> str.match(/one|two/g).sort().forEach(function f(x){(x != old) &&
> count++; old=x});
> // count is the result - test it.
>
> "count" is the number of different matches.
> If it is two you matched both.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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