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">jon
hiveminds.net> wrote:
>
> >
> >
> >
> >
> >
> >
> > > 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:Glob
al_Objects:RegExp
> > and
> >
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Glob
al_Objects:String:indexOf
> >
> > cheers
> >
> > 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("b" + word + "b");
> 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" value="Check" onclick="findWord('cne')">
> ...
> However when I escaped the b so it looked like "\b" + word
+ "\b",
> it worked. Fantastic.
> Thanx for the help.
> Dp.
>
> ------------------------------------
>
> Visit http://aiaiai.com for more groups to joinYahoo! Groups Links
>
>
>
>
--Tim Sabin
.