How do you make a regular expression that matches a word with one randomly inserted character?

Hi everyone, I want to use a regex to match a word with a single specified character, accidentally placed inside it. I also want to keep these “basic” word characters in their original order.

For example, with the "base" word testand the specified character 'y', I want the regular expression to match all of the following, and ONLY the following:ytest, tyest, teyst, tesyt, testy

This is important, im works in javascript and using the dojo toolkit.

Thank!

+3
source share
3 answers

Should it be a regex? If this is not so?

function matches(testWord, baseWord)
{
    for (var i =0; i < testWord.length; i++)
    {
    if(testWord.substr(0,i) + testWord.substr(i+1,testWord.length- i) == baseWord)
        return true;
}

return false;
}
+3

, , , \b(ytest|tyest|teyst|tesyt|testy)\b, , , .

, ,

\b(y)?t(y)?e(y)?s(y)?t(y)?\b

, - .

, , , . .

0

, (/d) ([xyz]), .

function matchPlus(string, base, plus){
    string= string.split(plus);
    return string.length== 2 && string.join('')== base;
}




//test case
var tA= 'ytest,tyest,teyst,test,ytesty,testyy,tesyt,testy'.split(','), L= 8;
while(L){
    tem= tA[--L];
    tA[L]= tem+'= '+!!matchPlus(tem,'test','y');
}

alert(testA.join('\n'))

/*  returned value: (String)
ytest= true
tyest= true
teyst= true
test= false
ytesty= false
testyy= false
tesyt= true
testy= true
*/
0

Source: https://habr.com/ru/post/1748240/


All Articles