List Info

Thread: Re: Re: Regex problem




Re: Re: Regex problem
country flaguser name
United States
2008-03-26 11:34:18

I agree with Jon, in that, if you have a specific misspelling you want
to correct, use a combination of indexOf and substr. For this, I would
use the string ' cne' (blank-c-n-e) to test for the misspelling.

var misspell = textToSearch.indexOf (' cne');
if (misspell != -1) {
textToSearch = textToSearch.substr (0, misspell) + ' acne' +
textToSearch.substr (misspell + 4);
}

--Tim Sabin

> 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.
> >
>; > 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:Glob
al_Objects:RegExp
> > and
> >
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Glob
al_Objects:String:indexOf
> >
>; > cheers
&gt; >
>; > jon.
>;
>
> Thanx for the reply Jon.
>;
> I am not sure that indexOf() will work for me.
>
> var stringToFind = 'cne';
&gt; 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.
&gt; Dp.
>
> ------------------------------------
&gt;
> Visit http://aiaiai.com for more groups to joinYahoo! Groups Links
&gt;
>
>
>

--Tim Sabin

__._,_.___
.

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

On 26/03/2008, Tim Sabin < tim%40timsabin.com">timtimsabin.com> wrote:
&gt;
>
>
&gt; I agree with Jon, in that, if you have a specific misspelling you want
>; to correct, use a combination of indexOf and substr. For this, I would
&gt; use the string ' cne' (blank-c-n-e) to test for the misspelling.
>
> var misspell = textToSearch.indexOf (' cne');
&gt; if (misspell != -1) {
> textToSearch = textToSearch.substr (0, misspell) + ' acne' +
> textToSearch.substr (misspell + 4);
> }

I can see how adding a leading space to the string might work but
isn't it possible that you could have a combination of a leading space
and letter that might hit substrig prior to the one you want. Example
for cne escape me but what about con or other common combinations of
letters.

I don't want to sound like I know the answer or have any experience on
this, I don't. It just seems to me the weakness of indexOf() is that
it will find the first instance which might not be what your looking
for.

A bit of background, the javascript is the frontend on a perl/aspell
script that is trying to find spelling mistakes in textareas and
prompt the user with alternatives to correct them. In the tests I have
done, "\b&quot; + document.forms[0].word.value + "\b&quot; has worked every
time whereas indexOf() hasn't.

Thanx,
Dp.

__._,_.___
.

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

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