Letter Replacer One Character Trouble
This is a continuation to the following question: customizable letter replacer I have a code that will replace letters, you can type whatever you want for a certain letter. For exa
Solution 1:
If you enter "chicken" and "nuggets" in the boxes to the right of "1," you get this regular expression:
/a|b|c|d|A|B|C|D|chicken|/g
Therefore, it will try to match anything containing an a, b, c, d, A, B, C, D, or chicken.
Since c comes before chicken in the regular expression's list, and chicken begins with a c, the word chicken will never bet matched.
What you should do is place the words before the letters in the regular expression. You can do that like this:
var re = newRegExp(
Object.keys(mapObj)
.sort(function(a, b) {
return b.length - a.length;
})
.join("|"),
"g"
);
The sort
function changes the order of the keys so that the longest words come first.
The regular expression will then become:
/chicken|a|b|c|d|A|B|C|D|/g
Snippet
// My globalsvar output = $("#output");
var extra_customizing = $("#extra-customizing");
String.prototype.cap = function () { // needed or demonstrationreturnthis.charAt(0).toUpperCase() + this.slice(1);
};
functiontrans() {
var input = $("#input");
var value = input.val();
// Retriving #customizing/*
I retrieve the values of the input boxes, in order to replace them later
*/// needed or demonstrationvar IDa = $("#a").val();
var IDb = $("#b").val();
var IDc = $("#c").val();
var IDd = $("#d").val();
// Retriving #extra-customizing/*
Using the same logic as the other ones
*/var ID_ax = $("#Ax").val(); // inputvar ID_ay = $("#Ay").val(); // outputvar ID_bx = $("#Bx").val(); // inputvar ID_by = $("#By").val(); // outputvar ID_cx = $("#Cx").val(); // inputvar ID_cy = $("#Cy").val(); // output/*
If the user inputs something to replace, they MUST have something to to replace it with(may change later)
*/if (ID_ax != "" && ID_ax == "") {
alert("You have not insterted a value in #1");
}
if (ID_bx != "" && ID_bx == "") {
alert("You have not insterted a value in #2");
}
if (ID_cx != "" && ID_cx == "") {
alert("You have not insterted a value in #3");
}
// Settingvar mapObj = {
// Setting #customizing/*
I first select what the user would write, and the what it should be replaced with
*/a: IDa,
b: IDb,
c: IDc,
d: IDd,
A: IDa.cap(),
B: IDb.cap(),
C: IDc.cap(),
D: IDd.cap()
};
// Extra customizing
mapObj[ID_ax] = ID_ay;
mapObj[ID_bx] = ID_by;
mapObj[ID_cx] = ID_cy;
// Translating/*
Below is the code used to replace letters
*/var re = newRegExp(
Object.keys(mapObj)
.sort(function(a, b) {
return b.length - a.length;
})
.join("|"),
"g"
);
console.log(re);
value = value.replace(re, function (matched) {
return mapObj[matched];
});
output.val(value);
}
body {
background-color: #cccccc;
color: #444444;
}
hr {
width: 60%;
background-color: #999999;
border: none;
padding: 0;
margin: 0 auto 0 auto;
}
#customizing {
font-family: "courier";
width: calc(50em + 195px);
width: -moz-calc(50em + 195px);
margin: auto;
font-size: .8em;
}
#extra-customizing {
font-family: "courier";
width: calc(55em + 282px);
width: -moz-calc(55em + 282px);
margin: auto;
font-size: .8em;
margin-top: .5em;
padding-top: .5em;
padding-left: .5em;
padding-right: .5em;
border-radius: 2px;
}
#customizinginput, #extra-customizinginput {
font-family: "courier";
width: 3em;
margin-left: 5px;
margin-right: 10px;
font-family: "courier";
text-align: center;
font-size: .8em;
padding-bottom: .3em;
padding-top: .2em;
background-color: #111111;
color: #aaaaaa;
border: none;
border-radius: 2px;
margin-bottom: 1em;
}
#extra-customizinginput {
margin-right: 15px;
}
#translator {
width: 100%;
}
#extra-customize {
width: 320px;
margin: .2em auto 1em auto;
}
#extra-customizeinput {
border: none;
padding: 0;
margin: 0;
width: 1em;
height: .9em;
}
#input {
width: 40%;
height: 40vh;
float: left;
padding: .43%;
margin: 0;
margin-left: 5%;
border: none;
background-color: #111111;
color: #aaaaaa;
border-radius: 2px;
font-size: 1em;
outline: none;
resize: none;
overflow: auto;
}
#inputB {
font-family: "courier";
width: 8.28%;
padding: 0;
margin: 0;
padding-top: 3px;
padding-bottom: 3px;
border: none;
background-color: #1f1f1f;
color: #aaaaaa;
border-radius: 2px;
font-size: .8em;
resize: none;
cursor: pointer;
outline: none;
}
#inputB:hover {
background-color: #aaaaaa;
color: #1f1f1f;
}
#output {
width: 40%;
height: 40vh;
float: right;
padding: .43%;
margin: 0;
margin-right: 5%;
border: none;
background-color: #111111;
color: #aaaaaa;
border-radius: 2px;
font-size: 1em;
outline: none;
resize: none;
overflow: auto;
}
</div><scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="customizing">
a<inputtype="text"id="a"value="a"maxlenght="3">
b<inputtype="text"id="b"value="b"maxlenght="3">
c<inputtype="text"id="c"value="c"maxlenght="3">
d<inputtype="text"id="d"value="d"maxlenght="3"></div><hr><divid="extra-customizing">
1<inputtype="text"id="Ax"value=""maxlength="7">:<inputtype="text"id="Ay"value=""maxlength="7">
2<inputtype="text"id="Bx"value=""maxlength="7">:<inputtype="text"id="By"value=""maxlength="7">
3<inputtype="text"id="Cx"value=""maxlength="7">:<inputtype="text"id="Cy"value=""maxlength="7"></div><divid="translator"><textareaid="input"></textarea><inputtype="button"value="Translate"id="inputB"onclick="trans()"><textareaid="output"readonly></textarea></div>
Post a Comment for "Letter Replacer One Character Trouble"