RegExp String-almost-right 
Author Message
 RegExp String-almost-right

Hi

Given a string searchtargey , which is almost the same as searchtarget, is
there a regular expression which tells you if a string is one character
different to another? Then there's searctarget, searchtargett etc...

Thanks for your time

Justin Dutoit



Wed, 23 Jul 2003 07:58:17 GMT  
 RegExp String-almost-right
I doubt if you can construct a regular expresssion to do it, but here's a function that will return
true if two strings only differ by a single character, and false otherwise.

function oneOff(str1,str2)
{
    if(Math.abs(str1.length - str2.length) != 1) return false;
    var bigger = str1.length > str2.length ? str1 : str2;
    var smaller = bigger == str1 ? str2 : str1;
    for(var a = 0; a < bigger.length; a++)
    {
        if(bigger.charAt(a) != smaller.charAt(a))
        {
           with(smaller) smaller = substr(0,a) + bigger.charAt(a) + substr(a);
           if(smaller == bigger) return true;
        }
    }
    return false;

Quote:
}


| Hi
|
| Given a string searchtargey , which is almost the same as searchtarget, is
| there a regular expression which tells you if a string is one character
| different to another? Then there's searctarget, searchtargett etc...
|
| Thanks for your time
|
| Justin Dutoit
|
|


Wed, 23 Jul 2003 19:35:31 GMT  
 RegExp String-almost-right
The other function is not completely accurate. Here's a revised version.

function oneOff(str1,str2)
{
    if(Math.abs(str1.length - str2.length) > 1 || str1 == str2) return false;
    var bigger = str1.length > str2.length ? str1 : str2;
    var smaller = bigger == str1 ? str2 : str1;
    var num = Math.abs(bigger.length == smaller.length);
    for(var a = 0; a < bigger.length; a++)
        with(smaller) if(bigger.charAt(a) != charAt(a))
           return bigger == substr(0,a) + bigger.charAt(a) + substr(a + num);

Quote:
}



| I doubt if you can construct a regular expresssion to do it, but here's a function that will
return
| true if two strings only differ by a single character, and false otherwise.
|
| function oneOff(str1,str2)
| {
|     if(Math.abs(str1.length - str2.length) != 1) return false;
|     var bigger = str1.length > str2.length ? str1 : str2;
|     var smaller = bigger == str1 ? str2 : str1;
|     for(var a = 0; a < bigger.length; a++)
|     {
|         if(bigger.charAt(a) != smaller.charAt(a))
|         {
|            with(smaller) smaller = substr(0,a) + bigger.charAt(a) + substr(a);
|            if(smaller == bigger) return true;
|         }
|     }
|     return false;
| }
|

| | Hi
| |
| | Given a string searchtargey , which is almost the same as searchtarget, is
| | there a regular expression which tells you if a string is one character
| | different to another? Then there's searctarget, searchtargett etc...
| |
| | Thanks for your time
| |
| | Justin Dutoit
| |
| |
|
|


Wed, 23 Jul 2003 20:45:17 GMT  
 RegExp String-almost-right
The reason I ask for a regexp is because it may have to be done on the
server side in vb..

Thanks anyhow
Justin



Quote:
> The other function is not completely accurate. Here's a revised version.

> function oneOff(str1,str2)
> {
>     if(Math.abs(str1.length - str2.length) > 1 || str1 == str2) return
false;
>     var bigger = str1.length > str2.length ? str1 : str2;
>     var smaller = bigger == str1 ? str2 : str1;
>     var num = Math.abs(bigger.length == smaller.length);
>     for(var a = 0; a < bigger.length; a++)
>         with(smaller) if(bigger.charAt(a) != charAt(a))
>            return bigger == substr(0,a) + bigger.charAt(a) + substr(a +
num);
> }



> | I doubt if you can construct a regular expresssion to do it, but here's

a function that will

- Show quoted text -

Quote:
> return
> | true if two strings only differ by a single character, and false
otherwise.
> |
> | function oneOff(str1,str2)
> | {
> |     if(Math.abs(str1.length - str2.length) != 1) return false;
> |     var bigger = str1.length > str2.length ? str1 : str2;
> |     var smaller = bigger == str1 ? str2 : str1;
> |     for(var a = 0; a < bigger.length; a++)
> |     {
> |         if(bigger.charAt(a) != smaller.charAt(a))
> |         {
> |            with(smaller) smaller = substr(0,a) + bigger.charAt(a) +
substr(a);
> |            if(smaller == bigger) return true;
> |         }
> |     }
> |     return false;
> | }
> |



- Show quoted text -

Quote:
> | | Hi
> | |
> | | Given a string searchtargey , which is almost the same as
searchtarget, is
> | | there a regular expression which tells you if a string is one
character
> | | different to another? Then there's searctarget, searchtargett etc...
> | |
> | | Thanks for your time
> | |
> | | Justin Dutoit
> | |
> | |
> |
> |



Thu, 24 Jul 2003 06:48:11 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. padding a string with spaces to the right

2. getting right 2 characters from a string

3. Convert string to RegExp pattern?

4. RegExp: how NOT to match string??

5. String evaluation from right problem

6. RegExp to validate string

7. Extract right part of a string

8. Regexp to match a subset of strings but exclude all of a certain character

9. RegExp: problems with comma-seperated string

10. zero-length string in RegExp

11. regexp matches unicode char in string

12. RegExp match of string with square bracket

 

 
Powered by phpBB® Forum Software