Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help creating regex

19 views
Skip to first unread message

Jo...@still_learning.invalid

unread,
Mar 20, 2013, 2:47:09 PM3/20/13
to
I know next to nothing about regex, but I need to determine whether a
decimal value is represented in a string.

Specifically, if the string 'XX' contains the phrase 7.4 within XX, I need
a test to come out true.

I've gone batty trying to do this, and I continually fail. Can someone
help?

Thanks in advance,
--
Joey

Danny

unread,
Mar 20, 2013, 3:08:46 PM3/20/13
to
To Joey

Give us the string example you're working with and the query you want, more specifically. To just get 2 numbers with a dot something like /\d\.\d/.test(YOURSTRINGHERE); will do.

Jim T.

unread,
Mar 20, 2013, 4:11:36 PM3/20/13
to
On Wed, 20 Mar 2013 11:47:09 -0700, "Joey@still_Learning.invalid"
<Joey@still_Learning.invalid> wrote:

>I know next to nothing about regex, but I need to determine whether a
>decimal value is represented in a string.
>
>Specifically, if the string 'XX' contains the phrase 7.4 within XX, I need
>a test to come out true.

Unless I misunderstood your question you don't need RegExp, indexOf()
will do.

Jo...@still_learning.invalid

unread,
Mar 20, 2013, 3:18:19 PM3/20/13
to
Danny wrote:

>To Joey
>
>Give us the string example you're working with and the query you want, more specifically. To just get 2 numbers with a dot something like /\d\.\d/.test(YOURSTRINGHERE); will do.

Thanks.
--
Joey

Jo...@still_learning.invalid

unread,
Mar 20, 2013, 3:19:07 PM3/20/13
to
You understood it correctly, and I resolved my issue using indexOf.
Thanks.
--
Joey

Dr J R Stockton

unread,
Mar 21, 2013, 4:20:31 PM3/21/13
to
In comp.lang.javascript message <vr2kk85pj3kfai9b9mu2qpuom24deooiol@4ax.
com>, Wed, 20 Mar 2013 12:19:07, "Joey@still_Learning.invalid"
<Joey@still_Learning.invalid> posted:
But there is, for most purposes, no decimal value 7.4 in the string
"aaa67.45zzz"; and, depending on circumstances, there is no decimal
value 7.4 in the string "7.4e5", although there is one in "7.4e0".

An ill-considered or inexact question will often, but not necessarily,
get an answer inappropriate for the purpose.

--
(c) John Stockton, nr London UK. ???@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Check boilerplate spelling -- error is a public sign of incompetence.
Never fully trust an article from a poster who gives no full real name.

Jo...@still_learning.invalid

unread,
Apr 27, 2013, 1:56:20 PM4/27/13
to
I know next to nothing about regex, but I need to determine whether an
exact match of a string within a string exists. My current code, which
doesn't work is:

firstString = 'testing';
testString = 'This is useful for testing cookies';

testTheStrings = testString.search(/\bfirstString\b/);

if (testTheStrings !=-1) {alert("Names are OK")}
else {alert("Names not the same");}

Keeps coming up with testTheStrings = -1.

What am I doing wrong?

TIA,
--
Joey

JJ

unread,
Apr 27, 2013, 3:27:57 PM4/27/13
to
What you did there is to search for "firstString" instead of "testing". So
the regex should be:

/\btesting\b/

i.e.: the text itself. not the variable name of the text you want to search.

So te code should be like this:

testTheStrings = testString.search(/\btesting\b/);

If you want to use a variable to pass to the search function...

firstString = /\btesting\b/;
testString = 'This is useful for testing cookies';
testTheStrings = testString.search(firstString);

For case-insensitive regex, use the "i" option:

firstString = /\btesting\b/i;

Stefan Weiss

unread,
Apr 27, 2013, 4:27:21 PM4/27/13
to
On 2013-04-27 19:56, Joey@still_Learning.invalid wrote:
> I know next to nothing about regex, but I need to determine whether an
> exact match of a string within a string exists. My current code, which
> doesn't work is:
>
> firstString = 'testing';
> testString = 'This is useful for testing cookies';
>
> testTheStrings = testString.search(/\bfirstString\b/);

In addition to what JJ wrote: if you're looking for an exact match and
don't really need the word boundary matches, you could also use indexOf():

var found = testString.indexOf("testing") > -1;

indexOf() can be a lot faster than a regular expression, depending on
the implementation. This example would also match "protesting" and
"testings". You can avoid that by searching for " testing ", if you know
that the word will be surrounded by spaces.

If you do need the regex, but are only interested in a true/false result
of the match, use the test() method instead of search():

var found = /\btesting\b/.test(testString);

It returns a boolean and is faster and arguably more expressive than
search().

If you want to match text stored in a variable, you will need to use the
RegExp() constructor:

var needle = "testing";
var haystack = "This is useful for testing cookies";
var regex = new RegExp("\\b" + needle + "\\b");
var found = regex.test(haystack);

In the third line, needle will be interpreted as part of a regular
expression, so you'll have to make sure that it doesn't contain any
characters that have a special meaning in regular expressions. Or you
can escape them using something like this:

function escapeRegex (str) {
return str.replace(/[\\^$+*?.(){}[\]]|-/g, "\\$&");
}

And once again, I wonder why the RegExp object doesn't have a built-in
method to do this...


One other thing: please don't reuse old threads for new questions. If
you have a new question, start a new thread.


- stefan

Stefan Weiss

unread,
Apr 27, 2013, 4:34:37 PM4/27/13
to
On 2013-04-27 22:27, Stefan Weiss wrote:
> return str.replace(/[\\^$+*?.(){}[\]]|-/g, "\\$&");

Typo: return str.replace(/[\\^$+*?.(){}[\]|-]/g, "\\$&");

- stefan

Jo...@still_learning.invalid

unread,
Apr 27, 2013, 7:39:17 PM4/27/13
to
Stefan Weiss wrote:

>On 2013-04-27 19:56, Joey@still_Learning.invalid wrote:
>> I know next to nothing about regex, but I need to determine whether an
>> exact match of a string within a string exists. My current code, which
>> doesn't work is:
>>
>> firstString = 'testing';
>> testString = 'This is useful for testing cookies';
>>
>> testTheStrings = testString.search(/\bfirstString\b/);
>
>In addition to what JJ wrote: if you're looking for an exact match and
>don't really need the word boundary matches, you could also use indexOf():
>
> var found = testString.indexOf("testing") > -1;
>
>indexOf() can be a lot faster than a regular expression, depending on
>the implementation. This example would also match "protesting" and
>"testings". You can avoid that by searching for " testing ", if you know
>that the word will be surrounded by spaces.
>
I can't use indexOf(), because there are spaces before, after and withing
the string.

>If you do need the regex, but are only interested in a true/false result
>of the match, use the test() method instead of search():
>
> var found = /\btesting\b/.test(testString);

Great. Thanks.
>
>It returns a boolean and is faster and arguably more expressive than
>search().
>
>If you want to match text stored in a variable, you will need to use the
>RegExp() constructor:
>
> var needle = "testing";
> var haystack = "This is useful for testing cookies";
> var regex = new RegExp("\\b" + needle + "\\b");
> var found = regex.test(haystack);
>
>In the third line, needle will be interpreted as part of a regular
>expression, so you'll have to make sure that it doesn't contain any
>characters that have a special meaning in regular expressions. Or you
>can escape them using something like this:
>
> function escapeRegex (str) {
> return str.replace(/[\\^$+*?.(){}[\]]|-/g, "\\$&");
> }
>
>And once again, I wonder why the RegExp object doesn't have a built-in
>method to do this...
>
Thanks for your response. I appreciate it.
>
>One other thing: please don't reuse old threads for new questions. If
>you have a new question, start a new thread.
>
No problem. (You keep these threads a long time.)

Thank you again.
--
Joey

Jo...@still_learning.invalid

unread,
Apr 27, 2013, 7:39:59 PM4/27/13
to
Thanks very much, JJ.
--
Joey

Evertjan.

unread,
Apr 28, 2013, 3:20:58 AM4/28/13
to
Joey@still_Learning.invalid wrote on 28 apr 2013 in comp.lang.javascript:

>> var found = testString.indexOf("testing") > -1;

[..]

> I can't use indexOf(), because there are spaces before, after and withing
> the string.

Invalid argument.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Jo...@still_learning.invalid

unread,
Apr 28, 2013, 4:22:07 AM4/28/13
to
Evertjan. wrote:

>Joey@still_Learning.invalid wrote on 28 apr 2013 in comp.lang.javascript:
>
>>> var found = testString.indexOf("testing") > -1;
>
>[..]
>
>> I can't use indexOf(), because there are spaces before, after and withing
>> the string.
>
>Invalid argument.

I don't agree. If the use inputs nothing, indexOf() will kick back a true,
when indeed the input is false.
--
Joey

Stefan Weiss

unread,
Apr 28, 2013, 5:32:41 AM4/28/13
to
I don't understand that sentence. Anyway, indexOf() by itself doesn't
return true or false, it returns the position of the first match, or -1
if there is no match. Hence the "> -1" in the example.

If the search string is empty, it will always match, because every
possible haystack string contains the empty string. That's a feature,
not a bug. If you don't want that, don't search for an empty string.

Spaces are treated exactly the same as any other character. I only
mentioned them because they are often used as word delimiters.

haystack | needle | indexOf() | indexOf > -1
-------------------+---------------+----------------+---------------
"abcde" | "ab" | 0 | true
"abcde" | "cd" | 2 | true
"abcde" | "ae" | -1 | false
"" | "" | 0 | true
"abcde" | "" | 0 | true
"" | "cd" | -1 | false
" " | "" | 0 | true
" " | " " | 0 | true
" a b c d e " | "c d" | 5 | true
" a b c d e " | " c d " | 4 | true
" a b c d e " | " " | 0 | true

Whenever you find yourself looking for an exact substring, indexOf() is
the first thing you should think about. You will only need a regex if
there are additional requirements, like: the needle must begin and end
at ANY word boundary, or the number of spaces in the needle is variable.


- stefan

Jo...@still_learning.invalid

unread,
Apr 28, 2013, 12:14:13 PM4/28/13
to
Additionally...were I to use indexOf() and the user entered test instead
of testing, I'd see a incorrect true response. That's what drove me to try
a regular expression to find a perfect match.

I'm very interested in why you believe my argument to be invalid. (Please)
--
Joey

Jo...@still_learning.invalid

unread,
Apr 28, 2013, 12:36:32 PM4/28/13
to
Thanks, again for this JJ. While it works as you have shown, it isn't
solving my dilemma. I need to amplify my issue...

Users enter 'firstString' into a form element. They later enter
'testString' into another form element. My task is to assure that
firstString is exactly contained within testString.

firstString = document.form.elements["firstString"].value;
testString = document.form.elements["testString"].value;

I tried your suggestion, i.e.,

firstString2 = /\bfirstString\b/;
testTheStrings = testString.search(firstString2);

But, as I'm sure you already know, it won't play.

Thanks again for your help.
--
Joey

Jo...@still_learning.invalid

unread,
Apr 28, 2013, 12:53:03 PM4/28/13
to
Stefan Weiss wrote:

>On 2013-04-28 10:22, Joey@still_Learning.invalid wrote:
>> Evertjan. wrote:
>>
>>>Joey@still_Learning.invalid wrote on 28 apr 2013 in comp.lang.javascript:
>>>
>>>>> var found = testString.indexOf("testing") > -1;
>>>
>>>[..]
>>>
>>>> I can't use indexOf(), because there are spaces before, after and withing
>>>> the string.
>>>
>>>Invalid argument.
>>
>> I don't agree. If the use inputs nothing, indexOf() will kick back a true,
>> when indeed the input is false.
>
>I don't understand that sentence.

Sorry, late at night entry. If the user elects to proceed without entering
anything, it returns a value instead of -1.

>Anyway, indexOf() by itself doesn't
>return true or false, it returns the position of the first match, or -1
>if there is no match. Hence the "> -1" in the example.

Agreed. I think in terms that -1 as false and any positive value is true.
>
>If the search string is empty, it will always match, because every
>possible haystack string contains the empty string. That's a feature,
>not a bug. If you don't want that, don't search for an empty string.

That would always be my preference, but some of my users are damned-fools
who hit the 'do-it' button without thinking. I want to catch their error.
>
>Spaces are treated exactly the same as any other character. I only
>mentioned them because they are often used as word delimiters.
>
> haystack | needle | indexOf() | indexOf > -1
>-------------------+---------------+----------------+---------------
> "abcde" | "ab" | 0 | true
> "abcde" | "cd" | 2 | true
> "abcde" | "ae" | -1 | false
> "" | "" | 0 | true
> "abcde" | "" | 0 | true
> "" | "cd" | -1 | false
> " " | "" | 0 | true
> " " | " " | 0 | true
> " a b c d e " | "c d" | 5 | true
> " a b c d e " | " c d " | 4 | true
> " a b c d e " | " " | 0 | true
>
>Whenever you find yourself looking for an exact substring, indexOf() is
>the first thing you should think about. You will only need a regex if
>there are additional requirements, like: the needle must begin and end
>at ANY word boundary, or the number of spaces in the needle is variable.
>
We have seen our users (who are medical professionals!) do the dumbest
things imaginable. They have been to enter one or more spaces preceding
and within the input strings, etc. : ' Jane Doe ' is typical. An even
more typical example is the user entering 'test' for one string and
'testing' or 'test id' or something similar for the test string. In either
example, indexOf() correctly returns a positive value, but it's an
erroneous response re my task.

I just answered JJ's response with an amplified explanation.

Thanks for your input.
--
Joey

Stefan Weiss

unread,
Apr 28, 2013, 1:41:29 PM4/28/13
to
On 2013-04-28 18:53, Joey@still_Learning.invalid wrote:
> Stefan Weiss wrote:
>>If the search string is empty, it will always match, because every
>>possible haystack string contains the empty string. That's a feature,
>>not a bug. If you don't want that, don't search for an empty string.
>
> That would always be my preference, but some of my users are damned-fools
> who hit the 'do-it' button without thinking. I want to catch their error.

Not a problem; you have to validate all user input anyway.

var str = prompt("Search for:").trim(); // or equivalent fallback
if (str) {
if (moreValidations(str)) {
// perform search
} else {
// display warning
}
} else {
// do nothing, treating no input as abort
}

> We have seen our users (who are medical professionals!) do the dumbest
> things imaginable. They have been to enter one or more spaces preceding
> and within the input strings, etc. : ' Jane Doe ' is typical.

So you're not looking to match exact strings... good to know.
If this is for a name search, you'll probably want separate fields for
first and last names.

trim() takes care of the spaces on both sides in " Jane Doe ", but if
you want to account for the spaces in the middle, you'll have to do more
work than \binput\b.

Even the \b markers can lead to incorrect matching in certain cases. For
example, \bRhys\b would incorrectly match Rhys-Davies. There are other
gotchas with \b as well (O'Neill, LeFêvre, ... all contain word boundaries).

In any case, if you're dynamically building a regex from user input, you
can start from the "new RegExp()" example I posted earlier.


- stefan

Jo...@still_learning.invalid

unread,
Apr 29, 2013, 2:51:21 AM4/29/13
to
Stefan Weiss wrote:

>On 2013-04-28 18:53, Joey@still_Learning.invalid wrote:
>> Stefan Weiss wrote:
>>>If the search string is empty, it will always match, because every
>>>possible haystack string contains the empty string. That's a feature,
>>>not a bug. If you don't want that, don't search for an empty string.
>>
>> That would always be my preference, but some of my users are damned-fools
>> who hit the 'do-it' button without thinking. I want to catch their error.
>
>Not a problem; you have to validate all user input anyway.
>
> var str = prompt("Search for:").trim(); // or equivalent fallback
> if (str) {
> if (moreValidations(str)) {
> // perform search
> } else {
> // display warning
> }
> } else {
> // do nothing, treating no input as abort
> }
>
>> We have seen our users (who are medical professionals!) do the dumbest
>> things imaginable. They have been to enter one or more spaces preceding
>> and within the input strings, etc. : ' Jane Doe ' is typical.
>
>So you're not looking to match exact strings... good to know.
>If this is for a name search, you'll probably want separate fields for
>first and last names

Actually, my users are selecting files to upload and I'm trying to
determine whether or not the name they entered is contained within the
filenames.
--
Joey

Jo...@still_learning.invalid

unread,
Apr 29, 2013, 4:40:05 AM4/29/13
to
Joey@still_Learning.invalid wrote:
>Joey@still_Learning.invalid wrote:
>>Evertjan. wrote:
>>>Joey@still_Learning.invalid wrote on 28 apr 2013 in comp.lang.javascript:
>>>
>>>>> var found = testString.indexOf("testing") > -1;

>>>> I can't use indexOf(), because there are spaces before, after and withing
>>>> the string.
>>>
>>>Invalid argument.
>>
>>I don't agree. If the use inputs nothing, indexOf() will kick back a true,
>>when indeed the input is false.

I was wrong! Thanks...you got me thinking (for a change it seems)
>
>Additionally...were I to use indexOf() and the user entered test instead
>of testing, I'd see a incorrect true response. That's what drove me to try
>a regular expression to find a perfect match.
>
>I'm very interested in why you believe my argument to be invalid. (Please)

The answer is so simple, I'm embarrassed having asked the question. :-)

firstString = document.form.elements["firstString"].value; //'Mary Smith'
testString = document.form.elements["testString"].value;
//Mary Smith-00003.img

adjName = testString.indexOf("-00");
endName = imageName0.substr(0,adjName);
testNames = endName.indexOf(firstString);
if (testNames != -1 && firstString == endName) {
alert("Identical")
}

From here, it's a simple task of identifying why the names are not
identical and alerting the user. With appropriate validation on
firstString input, I can filter out erroneous firstString inputs, e.g.,
double spaces. I have no control on validating testString, except the
knowledge its form is 'userinput-nnnnnm.img.'

The word doh! comes to my mind.

Thank you.
--
Joey

Jo...@still_learning.invalid

unread,
Apr 29, 2013, 4:41:54 AM4/29/13
to
Joey@still_Learning.invalid wrote:

>JJ wrote:
>
>Thanks, again for this JJ. While it works as you have shown, it isn't
>solving my dilemma. I need to amplify my issue...
>
Problem solved...just took some prodding to think. Thanks for your help.

firstString = document.form.elements["firstString"].value; //'Mary Smith'
testString = document.form.elements["testString"].value;
//Mary Smith-00003.img

adjName = testString.indexOf("-00");
endName = imageName0.substr(0,adjName);
testNames = endName.indexOf(firstString);
if (testNames != -1 && firstString == endName) {
alert("Identical")
}
--
Joey
0 new messages