find someChar inString

T

Totti

Hi everyone,
In on of the checks of my function check(), i need to check if in the
document.form1.textbox.value there are the Characters "CS" and thet
they are not the very first characters, so entereing let us say 101CS
is OK but CS101 is not, what is the simplest way to do it?
Best Regards, Totti
 
M

Martin Honnen

Totti said:
In on of the checks of my function check(), i need to check if in the
document.form1.textbox.value there are the Characters "CS" and thet
they are not the very first characters, so entereing let us say 101CS
is OK but CS101 is not, what is the simplest way to do it?

document.form1.textbox.value.indexOf('CS') > 0
 
M

Martin Honnen

Evertjan. said:
Martin Honnen wrote on 14 dec 2008 in comp.lang.javascript:


/.CS/.test(document.form1.textbox.value)

If you want to use a regualar expression then I think you need
/.+CS/.test(document.form1.textbox.value)
 
D

David Mark

If you want to use a regualar expression then I think you need
   /.+CS/.test(document.form1.textbox.value)

Best to use document.forms.form1.elements.textbox.value.
 
E

Evertjan.

Martin Honnen wrote on 14 dec 2008 in comp.lang.javascript:
If you want to use a regualar expression then I think you need
/.+CS/.test(document.form1.textbox.value)

You think wrong.
 
E

Evertjan.

kangax wrote on 14 dec 2008 in comp.lang.javascript:
/.CS/ still matches value such as "CS101---CS" which, IIUC, OP wants
to discard.

I did not read it like that, but it could be indeed.
Perhaps something along these lines would do better, although
it seems like using `indexOf` makes more sense in this case (and looks
more descriptive at that):

document.form1.textbox.value.indexOf('CS') > 0
would error on the same "CS101---CS"!
/^(?!CS).+CS.*/

the .* does not serve a purpose in test()
"CSCS" should be test()ed true

/^(CS|.).*CS/.test(document.form1.textbox.value)
 
W

William James

Evertjan. said:
kangax wrote on 14 dec 2008 in comp.lang.javascript:

and >>>> thet they are not the very first characters, so entereing
let us >>>> say 101CS is OK but CS101 is not, what is the simplest
way to do >>>> it?

I did not read it like that, but it could be indeed.


document.form1.textbox.value.indexOf('CS') > 0
would error on the same "CS101---CS"!


js> "CS101---CS".indexOf('CS') > 0
false
 
R

RobG

js> "CS101---CS".indexOf('CS') > 0
false

Indeed, per ECMA-262:

15.5.4.7 String.prototype.indexOf (searchString, position)

If searchString appears ... at one or more positions that
are greater than or equal to position, then the index of the smallest
such position is returned;
 
E

Evertjan.

kangax wrote on 14 dec 2008 in comp.lang.javascript:
kangax wrote:
[...]
Now that I look at my original regex, it could be written much shorter
and more efficiently as /(?!^)CS/. Literally, match "C" followed by "S"
but only if not preceded by a "beginning of line" anchor.

Nevermind that; The latter version still matches god damn "CS101----CS"
while former one seems to do just fine.

What former?

Please quote relevant all parts.
 
R

RobG

kangax wrote:

[...]
Now that I look at my original regex, it could be written much shorter
and more efficiently as /(?!^)CS/. Literally, match "C" followed by "S"
but only if not preceded by a "beginning of line" anchor.

Nevermind that; The latter version still matches god damn "CS101----CS"
while former one seems to do just fine.

If you really must use a RegExp and you also want to make it obvious
for maintainers, why not:

!/^CS/.test(s) && /CS/.test(s);
 
D

Dr J R Stockton

In comp.lang.javascript message <ff279564-70ab-4f67-8340-abe391464e74@b4
1g2000pra.googlegroups.com>, Sun, 14 Dec 2008 07:16:17, Totti <saliba.to
(e-mail address removed)> posted:
In on of the checks of my function check(), i need to check if in the
document.form1.textbox.value there are the Characters "CS" and thet
they are not the very first characters, so entereing let us say 101CS
is OK but CS101 is not, what is the simplest way to do it?


That is ambiguous, unless read carefully; and you may not want what you
asked for.

You ask that "CS" should appear anywhere in the substring which starts
at the second character. For that, using /^.+CS/ serves.

You may mean that there must be at least one "CS" and that the first two
characters must not be "CS". For that, use indexOf("CS")>0.

You might want there to be only one "CS" and that not at the start, but
you did not say that. For that, you could use .split("CS") and check
the length of that and of its first element; but IIRC that might be
browser-dependent. You could consider
A = S.indexOf("CS") ; A>0 && S.lastIndexOf("CS")!=A
 
E

Evertjan.

Dr J R Stockton wrote on 15 dec 2008 in comp.lang.javascript:
In comp.lang.javascript message <ff279564-70ab-4f67-8340-abe391464e74@b4
1g2000pra.googlegroups.com>, Sun, 14 Dec 2008 07:16:17, Totti <saliba.to
(e-mail address removed)> posted:



That is ambiguous, unless read carefully; and you may not want what you
asked for.

You ask that "CS" should appear anywhere in the substring which starts
at the second character. For that, using /^.+CS/ serves.

/.CS/ does.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
Martindap

Latest Threads

Top