Tengo una cadena y necesito verificar y obtener si las siguientes cadenas se superponen en mi cadena tagart con su final o inicio.
Cadena de destino: "haga clic en Ejecutar"
cadenas de búsqueda: "el botón Ejecutar para", "código y haga clic en"
Aparentemente:
"el botón Ejecutar para" se superpone al final del objetivo "haga clic en Ejecutar"
"codificar y hacer clic en" se superpone al comienzo del objetivo "hacer clic en Ejecutar"
Entonces "Ejecutar" y "hacer clic en" serán los resultados que necesito
Se me ocurrió una función para verificar y obtener los resultados superpuestos para los casos al principio y al final por separado.
Pregunta:
Pero mi código no podría obtener los resultados esperados solo si sé cómo la cadena de búsqueda se superpone con la cadena de destino en primer lugar. ¿Y cómo puedo combinar los resultados de la búsqueda de una sola vez?
function findOverlapAtEnd(a, b) { if (b.length === 2) { return ""; } if (a.indexOf(b) >= 0) { return b; } if (a.endsWith(b)) { return b; } return findOverlapAtEnd(a, b.substring(0, b.length - 1)); } function findOverlapAtStart(a, b) { if (b.length === 2) { return ""; } if (a.indexOf(b) >= 0) { return b; } if (a.startsWith(b)) { return b; } return findOverlapAtStart(a, b.substring(1)); } console.log(findOverlapAtEnd("click on the Run", "the Run button to")) console.log(findOverlapAtStart("click on the Run", "code and click on"))Debido a que necesito descomprimirme y estos acertijos lógicos me parecen divertidos, esta es mi solución al problema...
https://highdex.net/begin_end_overlap.htm
Puede ver la fuente de la página para ver el código JavaScript que utilicé. Pero en caso de que alguna vez elimine esa página, esta es la función importante...
function GetOverlappingSection(str1, str2, minOverlapLen = 4) { var work1 = str1; var work2 = str2; var w1Len = work1.length; var w2Len = work2.length; var resultStr = ""; var foundResult = false; var workIndex; if (minOverlapLen < 1) { minOverlapLen = 1; } else if (minOverlapLen > (w1Len > w2Len ? w2Len : w1Len)) { minOverlapLen = (w1Len > w2Len ? w2Len : w1Len); } //debugger; //we have four loops to go through. We trim each string down from each end and see if it matches either end of the other string. for (var i1f = 0; i1f < w1Len; i1f++) { workIndex = work2.indexOf(work1); if (workIndex == 0 || (workIndex != -1 && workIndex == w2Len - work1.length)) { //we found a match! foundResult = true; resultStr = work1; break; } work1 = work1.substr(1); if (work1.length < minOverlapLen) { break; } } if (!foundResult) { //debugger; //reset the work vars... work1 = str1; for (var i1b = 0; i1b < w1Len; i1b++) { workIndex = work2.indexOf(work1); if (workIndex == 0 || (workIndex != -1 && workIndex == w2Len - work1.length)) { //we found a match! foundResult = true; resultStr = work1; break; } work1 = work1.substr(0, work1.length - 1); if (work1.length < minOverlapLen) { break; } } } if (!foundResult) { //debugger; //reset the work vars... work1 = str1; for (var i2f = 0; i2f < w2Len; i2f++) { workIndex = work1.indexOf(work2); if (workIndex == 0 || (workIndex != -1 && workIndex == w1Len - work2.length)) { //we found a match! foundResult = true; resultStr = work2; break; } work2 = work2.substr(1); if (work2.length < minOverlapLen) { break; } } } if (!foundResult) { //debugger; //reset the work vars... work2 = str2; for (var i2b = 0; i2b < w2Len; i2b++) { workIndex = work1.indexOf(work2); if (workIndex == 0 || (workIndex != -1 && workIndex == w1Len - work2.length)) { //we found a match! foundResult = true; resultStr = work2; break; } work2 = work2.substr(0, work2.length - 1); if (work2.length < minOverlapLen) { break; } } } return resultStr; }Espero que eso sea útil.