Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expressions

Status
Not open for further replies.

dakkardaemor

Programmer
Apr 24, 2005
7
IT
Hi all!

I've a little question ;-)

I must create a regexp that matches only:
true|false|0|1

so I tryed with this one:
0|1|true|false

it seems to work well, but it matches also with:
truex falsehood and so on..

so I tried this one:
^(0|1|true|false)$
but this one doesn't seems to work as I supposed, it doesn't match with
true
for example :_(

I tried also:
^0|1|true|false$
but this one doesn't work as my first attempt...

In conclusion:
I think that
^(0|1|true|false)$
must be the solution, but I don't know why it doesn't work.

Please help!

Thank you in advance!

Dakkar
 
For single line test, try this pattern?
[tt] /^.*\b(0|1|true|false)\b.*$/gi[/tt]
- tsuji
 
Try putting parens around your alternatives:

[tt]^(0|1|true|false)$[/tt]



Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
thanks tsugi, but seems to not work with my javascript function to test them

function demoMatchClick() {
var re = new RegExp(document.demoMatch.regex.value);
if (document.demoMatch.subject.value.match(re)) {
alert("Successful match");
} else {
alert("No match");
}
}

:_(


Thanks also to you tsdragon, but your expression that seems correct me too, doesn't work! :_(

Thank you anyway friends, if you have some other ideas, please post them!

 
dakkardaemor,

The code tsuji posted was intended to be executed as such:

Code:
  var myRE = /^.*\b(0|1|true|false)\b.*$/gi;

  // then use as normal..
  alert(myRE.test(document.demoMatch.subject.value));

the / at each end is the start and end delimiter, the gi are switches - try the above or try the following in your own setup (where you take it from a text box on the page).

Code:
   ^.*\b(0|1|true|false)\b.*$
Type the above into the text box, this should then work as expected.

Good luck...

A smile is worth a thousand kind words. So smile, it's easy! :)
 
ahhhh! ^_^
GREAT! ^_^

Thank you friends!!!

I'm really really happy!
Thank you!

I hope to help you if you need as you helped me!

Thanks!

Dakkar Daemor
Ytanium reviewer
Freeware Fan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top