Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #14738
| Newsgroups | comp.lang.javascript |
|---|---|
| Subject | Re: reg expression |
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
| References | <jsfv2r$sks$1@speranza.aioe.org> <q1gnu759m1k9lbf8jdqqcam93edn4csn82@4ax.com> |
| Message-ID | <XnsA080557FD310Deejj99@194.109.133.133> (permalink) |
| Date | 2012-06-28 06:24 +0000 |
Gene Wirchenko wrote on 28 jun 2012 in comp.lang.javascript:
> On Wed, 27 Jun 2012 17:48:15 -0400, "phil hunt" <a@abc.com> wrote:
>
>>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.
>
> If you mean that the string contains both "one" and "two", the
> regex would be
> one.*two|two.*one
That's the idea!
Whatsay, John?
> Warning: "." does not match newline. It does match any other
> character.
I suggest when seperate words are ment,
and give a multiline alternative:
================================
function myTest(text) {
r = text.replace(/\n/,'[newline]') + ' -> '
r += /\bone\b.*\btwo\b|\btwo\b.*\bone\b/.test(text);
r += '<br>'
document.write( r );
};
myTest('two one');
myTest('one two');
myTest('onetwo');
myTest('two ones');
myTest('two \n one');
function myTestMulti(text) {
r = text.replace(/\n/,'[newline]') + ' -> '
r += /\bone\b[\s\S]*\btwo\b|\btwo\b[\s\S]*\bone\b/.test(text);
r += '<br>'
document.write( r );
};
myTestMulti('two \n one');
=================================
two one -> true
one two -> true
onetwo -> false
two ones -> false
two [newline] one -> false
two [newline] one -> true
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
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