A problem occurs at work. Two strings match. The requirement is that each string contains at most one *,? Unlimited
* Represents a string of any length, and? Represents a character
Two conflicting requests are prompted.
<Input type = "text" id = "str1"> <br>
<Input type = "text" id = "str2"> <br>
<Input type = "button" onclick = "checkMarchX ()" value = "CHECK">
The Javascript code is as follows:
[Javascript]
Function checkMarchX ()
{
Var str1 = document. getElementById ('str1'). value;
Var str2 = document. getElementById ('str2'). value;
Var str1XPosition = str1.indexOf ('*');
Var str2XPosition = str2.indexOf ('*');
If (str1XPosition! =-1 & str2XPosition! =-1) // both contain *
{
Var position = str1XPosition> str2XPosition? Str2XPosition: str1XPosition; // success
If (position! = 0)
{
Var patbeforeStr1 = str1.substring (0, position );
Var patbeforeStr2 = str2.substring (0, position );
If (checkMarchQ (patbeforeStr1, patbeforeStr2 ))
{
// Alert (str1 + 'conflicts with' + str2 + "first half ");
// Perform the test in the second half.
Var str1XBackPosition = str1.length-str1XPosition-1;
Var str2XBackPosition = str2.length-str2XPosition-1;
Var backposition = str1XBackPosition> str2XBackPosition? Str2XBackPosition: str1XBackPosition;
If (backposition = 0)
{
Alert (str1 + 'conflicts with' + str2 + ");
}
Else
{
Var patbackStr1 = str1.substring (str1.length-backposition, str1.length );
Var patbackStr2 = str2.substring (str2.length-backposition, str2.length );
If (checkMarchQ (patbackStr1, patbackStr2 ))
{
Alert (str1 + 'conflicts with' + str2 + ");
}
}
}
}
Else
{
// Alert (str1 + 'conflicts with' + str2 + "first half ");
Var str1XBackPosition = str1.length-str1XPosition-1;
Var str2XBackPosition = str2.length-str2XPosition-1;
Var backposition = str1XBackPosition> str2XBackPosition? Str2XBackPosition: str1XBackPosition;
If (backposition = 0)
{
Alert (str1 + 'conflicts with' + str2 + ");
}
Else
{
Var patbackStr1 = str1.substring (str1.length-backposition, str1.length );
Var patbackStr2 = str2.substring (str2.length-backposition, str2.length );
If (checkMarchQ (patbackStr1, patbackStr2 ))
{
Alert (str1 + 'conflicts with' + str2 + ");
}
}
}
}
Else if (str1XPosition =-1 & str2XPosition! =-1) | (str1XPosition! =-1 & str2XPosition =-1) // only one string contains *
{
Var strX = str1XPosition =-1? Str2: str1; // string containing *
Var strNoX = str1XPosition =-1? Str1: str2; // a string not including *
If (strX. length-1 <strNoX. length)
{
Var position = strX. indexOf ('*');
If (position = 0)
{
// Alert (str1 + 'conflicts with' + str2 + "first half ");
Var backposition = strX. length-position-1;
If (backposition = 0)
{
Alert (str1 + 'conflicts with' + str2 + ");
}
Else
{
Var patbackStr1 = str1.substring (str1.length-backposition, str1.length );
Var patbackStr2 = str2.substring (str2.length-backposition, str2.length );
If (checkMarchQ (patbackStr1, patbackStr2 ))
{
Alert (str1 + 'conflicts with' + str2 + ");
}
}
}
Else
{
Var patbeforeStr1 = str1.substring (0, position );
Var patbeforeStr2 = str2.substring (0, position );
If (checkMarchQ (patbeforeStr1, patbeforeStr2 ))
{
// Alert (str1 + 'conflicts with' + str2 + "first half ");
Var backposition = strX. length-position-1;
If (backposition = 0)
{
Alert (str1 + 'conflicts with' + str2 + ");
}
Else
{
Var patbackStr1 = str1.substring (str1.length-backposition, str1.length );
Var patbackStr2 = str2.substring (str2.length-backposition, str2.length );
If (checkMarchQ (patbackStr1, patbackStr2 ))
{
Alert (str1 + 'conflicts with' + str2 + ");
}
}
}
}
}
}
Else
{
If (checkMarchQ (str1, str2 ))
{
Alert (str1 + 'conflicts with' + str2 + ");
}
}
}
Function checkMarchQ (str1, str2)
{
Var flagque = false;
If (str1.length = str2.length)
{
// Conflict is possible only when the length is the same
For (var I = 0; I <str1.length; I ++)
{
If (str1.substr (I, 1 )! = '? '& Str2.substr (I, 1 )! = '? ')
{
If (str1.substr (I, 1 )! = Str2.substr (I, 1 ))
{
Flagque = false; // indicates no conflict
Break;
}
Else
{
Flagque = true; // conflict
}
}
}
}
Return flagque;
}
* At least one character is entered. The above program does not verify the validity of the input.