Work encountered a problem, two string matching, required: Each string contains a maximum of *,? can be infinitely many
* Represents a string of any length, while? Represents a character
The request can prompt two conflicts
Copy Code code as follows:
<input type= "text" id= "str1" ><br>
<input type= "text" id= "str2" ><br>
<input type= "button" onclick= "Checkmarchx ()" value= "CHECK" >
Implement the code in JavaScript as follows:
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+ ' and ' +str2+ ' the first half of the conflict ");
Then the second part is tested var str1xbackposition = str1.length-str1xposition-1;
var str2xbackposition = str2.length-str2xposition-1;
var backposition = str1xbackposition>str2xbackposition?str2xbackposition:str1xbackposition;
if (backposition==0) {alert (str1+ ' and ' +str2+ ' conflict ');
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+ ' and ' +str2+ ' conflict);
}} else {//alert (str1+ ' and ' +str2+ ' first half conflict);
var str1xbackposition = str1.length-str1xposition-1;
var str2xbackposition = str2.length-str2xposition-1;
var backposition = str1xbackposition>str2xbackposition?str2xbackposition:str1xbackposition;
if (backposition==0) {alert (str1+ ' and ' +str2+ ' conflict ');
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+ ' and ' +str2+ ' conflict); else if (str1xposition==-1 && str2xposition!=-1) | | (Str1xposition!=-1 && str2xposition==-1))
There is and only one string containing * {var Strx = str1xposition==-1?str2:str1;//containing * string var Strnox = str1xposition==-1?str1:str2;//string without * if (Strx.length-1<strnox.length) {var position = strx.indexof (' * ');
if (position==0) {//alert (str1+ ' and ' +str2+ "conflict");
var backposition = strx.length-position-1;
if (backposition==0) {alert (str1+ ' and ' +str2+ ' conflict ');
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+ ' and ' +str2+ ' conflict);
}} else {var patbeforeStr1 = str1.substring (0,position);
var patbeforeStr2 = str2.substring (0,position);
if (CHECKMARCHQ (PATBEFORESTR1,PATBEFORESTR2)) {//alert (str1+ ' and ' +str2+ ' the first half of the conflict ");
var backposition = strx.length-position-1;
if (backposition==0) {alert (str1+ ' and ' +str2+ ' conflict ');
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+ ' and ' +str2+ ' conflict);
}}}}} else {if (CHECKMARCHQ (STR1,STR2)) {alert (str1+ ' conflict ' with ' +str2+ ');
}} function Checkmarchq (STR1,STR2) {var flagque = false; if (str1.length==str2.length) {//length is the same as possible conflict for (Var i=0; I<str1.length i++) {if (str1.substr) i,1 '? '!=
mp;& str2.substr (i,1)!= '? ')
{if (Str1.substr (i,1)!=str2.substr (i,1)) {Flagque = false;//indicates no conflict break;
else {Flagque = true;//indicates conflict}}} return flagque;
}
where * at least one character, the above program does not validate the legality of the input
See more JavaScript syntax, you can focus on: JavaScript reference tutorial, JavaScript Code style guide, and also hope that many people support the cloud-Habitat community.