List Info

Thread: Re: Regex problem




Re: Regex problem
country flaguser name
United States
2008-03-26 01:58:09


> Regex problem
> Posted by: "Dermot" paikkos%40googlemail.com">paikkosgooglemail.com dermotduke
> Date: Tue Mar 25, 2008 9:29 am ((PDT))
>
> Hi,
>
> I am struggling to create a Regex with boundies "/b" in. I need to search a
> textarea and make an exact match for a word. So a search for 'abc' must not
> find 'abcc'. I should be simple and there are methods for it but I am not
> sure how to construct the Regex. I am having to use this format:
>
> var pattern = new RegExp(document.forms[0].word.value);
>
> when I really want to do
>
> var pattern = /bdocument.forms[0].word.valueb/;
>
> The problem with the latter is that it is treated like a literal '
> document.forms[0].word.value' as opposed to the value.
>
> Can anyone offer some advice on how to do this?

Assemble the text of the regular expression as a string and pass this string to
the RegExp constructor:

var pattern = new RegExp("b" + document.forms[0].word.value + "b");

However, if you simply need an exact match for a string, then using a regular
expression is overkill - the reason for using regular expressions is to match
*patterns*, not string literals.

In this case, you can use the indexOf() method of String instead:

var stringToFind = "abc";
var textToSearch = document.forms[0].word.value;
var msg = "";

if(textToSearch.indexOf(stringToFind) != -1)
msg = "String found";;
else
msg = "String not found";;

alert(msg);

See
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:RegExp
and
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:String:indexOf

cheers

jon.

> Thanx,
> Dp.

--
This message has not been scanned for viruses.

Since I do not use a Microsoft operating
system or software, and use only plaintext
for email, there is little need for me to do so.

__._,_.___
.

__,_._,___
Re: Re: Regex problem
country flaguser name
United States
2008-03-26 09:34:12

On 26/03/2008, Jon Stephens < jon%40hiveminds.net">jonhiveminds.net> wrote:

>
&gt;
>
>
>;
>
> > Hi,
> >
>; > I am struggling to create a Regex with boundies "/b&quot; in. I need to search a
> > textarea and make an exact match for a word. So a search for 'abc' must not
> > find 'abcc'. I should be simple and there are methods for it but I am not
> > sure how to construct the Regex. I am having to use this format:
> >
>; > var pattern = new RegExp(document.forms[0].word.value);
> >
>; > when I really want to do
> >
>; > var pattern = /bdocument.forms[0].word.valueb/;
> >
>; > The problem with the latter is that it is treated like a literal '
> > document.forms[0].word.value' as opposed to the value.
&gt; >
>; > Can anyone offer some advice on how to do this?
&gt;
> Assemble the text of the regular expression as a string and pass this string to
> the RegExp constructor:
>
> var pattern = new RegExp(&quot;b" + document.forms[0].word.value + "b&quot;);
>;
> However, if you simply need an exact match for a string, then using a regular
> expression is overkill - the reason for using regular expressions is to match
&gt; *patterns*, not string literals.
>
&gt; In this case, you can use the indexOf() method of String instead:
>
>; var stringToFind = "abc&quot;;
&gt; var textToSearch = document.forms[0].word.value;
> var msg = "&quot;;
>
> if(textToSearch.indexOf(stringToFind) != -1)
> msg = "String found";;
> else
>; msg = "String not found";;
>
> alert(msg);
>
> See
> http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:RegExp
> and
> http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:String:indexOf
>;
> cheers
&gt;
> jon.

Thanx for the reply Jon.

I am not sure that indexOf() will work for me.

var stringToFind = 'cne';
var textToSearch = 'The most common form of acne is cne vulgaris';

In this case indexof would return 25, the 'cne' in 'acne'. Whereas I
want the misspelt cne at position 33. That's why I thought that
putting some boundaries on the string would anchor it and ensure that
it found the right one. I have tried to build up the string as you
suggest but my attempts are failing

function findWord(word) {
var pattern = new RegExp(&quot;b" + word + "b&quot;);
var origText = document.forms[0].caption.value;
var result = origText.replace(pattern,'acne');
alert(word+" "+;result);
document.forms[0].caption.value = result;
}

...
<input type=";button&quot; value=&quot;Check&quot; onclick=&quot;findWord('cne')&quot;>
...
However when I escaped the b so it looked like "\b&quot; + word + "\b&quot;,
it worked. Fantastic.
Thanx for the help.
Dp.

__._,_.___
.

__,_._,___
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )