I want that the code reacts to an input by looking at first at the whole word.
If the code can't find a matching substring it looks only at the first four letters and then matches it with a word, that has the same first four letters.
This is what I have tried so far:
function FT() {
var str = document.getElementById("ip").value;
var res = str.split(" ");
document.getElementById("splited").innerHTML = res;
var arr = str.replace("bankcard", "finance");
var str = document.getElementById("arranged").innerHTML = arr
// This part should not exist
var arr = str.replace("bankrupt", "finance");
var str = document.getElementById("arranged").innerHTML = arr
// The part above was added only to get the right result
var arr = str.replace("cartwheel", "vehicle");
var str = document.getElementById("arranged").innerHTML = arr
// This part should not exist
var arr = str.replace("carthorse", "vehicle");
var str = document.getElementById("arranged").innerHTML = arr
// The part above was added only to get the right result
var arr = str.replace("bookmark", "books");
var str = document.getElementById("arranged").innerHTML = arr
// This part should not exist
var arr = str.replace("bookshop", "books");
var str = document.getElementById("arranged").innerHTML = arr
// The part above was added only to get the right result
}
<textarea id="ip">bankrupt carthorse bookshop </textarea>
<button onclick="FT()"> match </button>
<p id="splited"></p>
<p id="arranged"></p>
Result for the input: "bankrupt carthorse bookshop" should be: finance vehicle books
Because:
bankrupt has the same first 4 letters like bankcard - so it gets matched with finance
carthorse has the same first 4 letters like cartwheel - so it gets matched with vehicle
bookshop has the same first 4 letters like bookmark - so it gets matched with books
x
? – pavel Nov 25 '20 at 21:17str.startsWith("ABCD")
. But I don't really get what problem you are trying to solve. – Bergi Nov 25 '20 at 21:22