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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with RegExp...

Status
Not open for further replies.

Markh51

Programmer
May 20, 2003
81
0
0
GB
Ok, up to now everthing works with my RegExp:

^[a-zA-Z]+[- a-zA-Z]?[a-zA-Z]+$

I can enter:

the cat
the-cat

but not:
the cat sat
the-cat-sat

I need to modify the above RegExp so you can have Spaces or "-" WITHIN a sentence BUT not in pairs or more, only by themselves

i.e.

the-cat-sat
the cat sat

NOT:

the--cat-sat
the cat sat

Thanks in advance.
 
empty space = \s
so you can just do a \s- between the (\w) char's

1H 1K 10 3D 3F 3E 3K 38 3J 10 1T 10 3G 3L 3I 35 10 35 3O 33 35 3C 3C 35 3E 33 35

brickyard.jpeg
Most intense event I ever attended!
 
The one above has a mistake but this I think works-

var reg = /[^a-zA-Z\s-]|(\s\s)|(--)/g;
 
Mark,

Try 2 tests like:
function tst() {
var tststra = /^[a-zA-Z -]+$/;
var tststrb = /( )|(--)/g;
if(document.form1.testval.value.search(tststra)> -1){
if(document.form1.testval.value.search(tststrb)> -1) {
alert("Too many whatchamacllits entered!");}
else { alert("Finally you got it right!"); }
}
else { alert("Stop goofing off and enter the right char's!");}
}

2b||!2b
 
One other scenario:

You might add " -" to the string to exclude like:

/( )|(--)|(- )|( -)/g

2b||!2b
 
what does the /g do on the end of the expression ?
 
I believe it is possible to do it with one regExp. See my last post above.
 
/g

Indicates that the parameters apply "globally"
that is the whole string from begginnin to end.

Specify what doesn't work.


2b||!2b
 
Well, If i use my original Exp:

^[a-zA-Z]{1,}[- a-zA-Z]{0,1}[a-zA-Z]{1,}$

OR

^[a-zA-Z]+[- a-zA-Z]?[a-zA-Z]+$

It works great, I can however only use ONE space or "-" throught the whole sentence, I need to modify the above Expression so it "repeats", so I can use more than ONE (but NOT together) SPACES or "-".

Thanks
 
I tested it and it seems to work. Can you give me an example of what dosen't work with it? Or anyone else?
 
what did you test, my RegEx or your's ??

If it was mine, you can't enter:

word word word OR word-word-word

ONLY

word word OR word-word

Thanks
 
Thanks scriptOhead,
Mark,

function tst1() {
var tststr = /[^a-zA-Z\s-]|(\s\s)|(--)/g;
if(document.form1.testval.value.search(tststr) == -1)
{ alert("Correct!");}
else { alert("Try Again!");}
}

His (scriptOhead's) test string is absolutely your best solution. He's using a not(^) test.

It looks for [^a-zA-Z\s-] any chr that's not a-zA-Z " " or -
he uses |(or) testing for double " " or -

I would suggest:
/[^a-zA-Z\s-]|(\s\s)|(--)|(\s-)|(-\s)/g
so that you eliminate " -" & "- " possibilities.
Load this page and give it a try....

<html>
<head>
<title>TEST Engine</title>
<script type=&quot;text/javascript&quot; language=&quot;JavaScript1.2&quot;>
function tst1() {
var tststr = /[^a-zA-Z\s-]|(\s\s)|(--)|(\s-)|(-\s)/g;
if(document.form1.testval.value.search(tststr) == -1){
alert(&quot;Correct!&quot;);}
else { alert(&quot;Try Again!&quot;);}
}
</script>
</head>
<body>
<form name=&quot;form1&quot;><input type=&quot;text&quot; name=&quot;testval&quot; size=&quot;15&quot;
value=&quot;entry please&quot;></form>
<a href=&quot;javascript:tst1()&quot;>TEST</a>
</body>
</html>

Thanks scriptO,



2b||!2b
 
scriptOhead,

With your RegEx it works OK but if I enter:

&quot;word--word&quot; it will work, your RegEx only seems to NOT let you put more than one space after each other, it doesn't work for the &quot;-&quot;.

Cheers.
 
Lrnmore,

What the hell are the rolling heads in your code supposed to mean ?

Mark
 
Wait a minute.....
Who put those smiley faces in there????

I don't think it will work with that.......

I guess you can't enter '(\s-)'
Anyhow replace them with that!

2b||!2b
 
try the [ code ] tags around your code or turn off process TGML by unchecking it guys

1H 1K 10 3D 3F 3E 3K 38 3J 10 1T 10 3G 3L 3I 35 10 35 3O 33 35 3C 3C 35 3E 33 35

brickyard.jpeg
Most intense event I ever attended!
 
It's not me I swear.....

I thought the single quotes would keep it from
happening...

O.K. I should have unchecked that Smiley box
right above submit post I've got it now.
Let's try it again....

What I'm trying to say is use this:
/[^a-zA-Z\s-]|(\s\s)|(--)|(\|(-\s)/g





2b||!2b
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top