Replace character of a string with the character from an array - javascript

I'm trying to make a script that changes text into these cool looking letters, it looks like everything should be working but when I try to send the replaced message it gives me a character like a white question mark on a black background. This: �
Here's the code:
var mm = "test";
var alphabet = "🇦🇧🇨🇩🇪🇫🇬🇭🇮🇯🇰🇱🇲🇳🇴🇵🇶🇷🇸🇹🇺🇻🇼🇽🇾🇿";
var nalphabet = "abcdefghijklmnopqrstuvwxyz";
for(var z in mm){
var x = nalphabet.indexOf(mm[z].toLowerCase());
var ool = alphabet[x];
msg.channel.sendMessage(ool);
}

This is all about the coding of characters. Each of your cool-looking letters has length 2. So when you try to get such character directly by index, you receive just a half of it. As a solution you can try to join two sibling characters. Something like this
var mm = "test";
var alphabet = "🇦🇧🇨🇩🇪🇫🇬🇭🇮🇯🇰🇱🇲🇳🇴🇵🇶🇷🇸🇹🇺🇻🇼🇽🇾🇿";
var nalphabet = "abcdefghijklmnopqrstuvwxyz";
for(var z in mm){
var x = nalphabet.indexOf(mm[z].toLowerCase());
var ool = alphabet[x * 2] + alphabet[x * 2 + 1];
msg.channel.sendMessage(ool);
}

Try this one:
var message = "test";
var alphabet = ["🇦","🇧","🇨","🇩","🇪",
"🇫","🇬","🇭","🇮","🇯",
"🇰","🇱","🇲","🇳","🇴",
"🇵","🇶","🇷","🇸","🇹",
"🇺","🇻","🇼","🇽","🇾","🇿"];
var nalphabet = "abcdefghijklmnopqrstuvwxyz";
for(var letter in message) {
var x = nalphabet.indexOf(message[letter].toLowerCase());
var ool = alphabet[x];
document.write(ool);
}
Here is a Fiddle.

Related

How to split math calculations in javascript

I have mathematical calculations in div tag, like that:
13*7=91
So how to split and parse data?
and it will stored in variables like that:
var A = 13;
var Operation = '*';
var B = 7;
var Result = 91;
please tell me how to make that :)
You can split it first by = sign, and then by possible math signs, for example:
var s = '13*7=91';
var a = s.split('=');
var b = a[0].split(/[\+\-\*\/\^%]/);
var A = b[0];
var B = b[1];
var Operation = a[0].replace(A,'').replace(B,'');
var Result = a[1];
console.log(A+Operation+B+'='+Result);
Output:
13*7=91
This is an easy way of doing it, simply using RegExp.
The first one is /[0-9]+/g to take the operands and the result numbers and the second one is /[0-9]+(.)[0-9]+/ to extract the operator, then I print the result in a diplay p elemnt:
var str = document.getElementById("calcul").innerText;
var re = /[0-9]+/g;
var re2 = /[0-9]+(.)[0-9]+/;
var operands = str.match(re);
var operator = str.match(re2)[1];
var A = operands[0];
var B = operands[1];
var result = operands[2];
var display = document.getElementById("display");
display.innerHTML = "var A = " + operands[0] + "<br>var B = " + operands[1] + "<br>var result = A" + operator + "B =" + result;
<div id="calcul">
13*7=91
</div>
<br>Calculation results :
<p id="display">
</p>
Maybe you can try something like this:
Make a regular expression that detects the numbers separated by anything (+, *, -, /, =, etc).
Make that regular expression detects the separating elements.
Then execute eval() in javascript. Be careful with this.
When you have a piece of code show us and we can help you better.
Good luck.

Need to strip a leading character (# or +) from this string

In the cases below I need to strip either the first "#" or "+" character for vars b & c so they can become vars a & a1 which are used to be a string in a new url that's built elsewhere.
Here is the code:
var b = "http://www.somewhere.com/search/#foo+bar+baz"
var c = "http://www.somewhere.com/search/++bar+baz"
var a = b.split("/")[4].split("+").slice(0, 1);
var a1 = c.split("/")[4].split("+").slice(0, 1);
Here is the fiddle.
This works for me on your fiddle.
var b = "http://www.somewhere.com/search/#foo+bar+baz"
var c = "http://www.somewhere.com/search/++bar+baz"
var a = b.split("/")[4].split("+").slice(0, 1);
if(a[0].indexOf('#') != -1){a = a[0].split('#')[1]}
var a1 = c.split("/")[4].split("+");
var i=0;
while(true){
if(a1[i] != ''){a1 = a1[i]; break;}
i++;
}
$("#result").html(a);
$("#result2").html(a1);
console.log(a);
console.log(a1);
Updated your fiddle
You can replace the first occurrence of a char with using a regex quite easily:
var a = b.replace(/[#+]/,'');
var a1 = c.replace(/[#+]/,'');
Without the g modifier it will only replace the first one it finds.

find first number in a string followed by x amount of characters

string sChar = "_$$$ASDF 123-456-789123123XXX";
string sChar = "$$VIC123-456-789pppEEX";
I would like to parse the above examples of sChar to result in the following value
123-456-789
What this regex would do is find the first Number in the string as well as the next 10 characters. The next 10 characters can be special characters, alpha, or numberic.
Here the solution for you:
var sChar = "_$$$ASDF 123-456-789123123XXX";
//string sChar = "$$VIC123-456-789pppEEX";
var indexDigit = sChar.search(/[\d]/);
var str = sChar.substring(indexDigit, indexDigit+11);
alert(str);
I see an answer like this:
var str = sChar.match(/\d.{10}/);
alert(str)
That won't work:
Try the following:
var sChar = "_$$$ASDF 123-4$6-7";
var sChar2 = "$$VIC987-6$4-3";
var indexDigit = sChar.search(/[\d]/);
var str = sChar.substring(indexDigit, indexDigit+11);
alert(str);//returns "123-4$6-7"
var str2 = sChar2.match(/\d.{10}/);
alert(str2);//returns null

JavaScript get character in sting after [ and before ]

I have some strings like:
str1 = "Point[A,B]"
str2 = "Segment[A,B]"
str3 = "Circle[C,D]"
str4 = "Point[Q,L]"
Now I want to have function that gives me character after "[" and the character before "]". How could I make something like that ?
try this one...
var str = "Point[A,B]";
var start_pos = str.indexOf('[') + 1;
var end_pos = str.indexOf(']',start_pos);
var text_to_get = str.substring(start_pos,end_pos)
alert(text_to_get);
You'd need regex to do that
var matches = /\[(.*?)\]/.exec(str1);
alert(matches[1]);
You can use match() to extract the characters:
str.match(/\[(.*)\]/)[1]
A safer way would be:
var matches = str.match(/\[(.*)\]/);
if(matches) {
var chars = matches[1];
}
Here's an approach which avoids regex.
var str = "Point[A,B]";
var afterOpenBracket = str.split("[")[1]; // returns "A,B]"
var bracketContents = afterOpenBracket.split("]")[0]; // returns "A,B"
There, pretty simple! bracketContents now contains the entirety of the text between the first set of brackets.
We can stop here, but I'll go a step further anyway and split up the parameters.
var parameters = bracketContents.split(","); // returns ["A", "B"]
Or in case u have more [A,C,D,B] and don't want to use regex:
var str1 = "Point[A,C,D,B]";
function extract(str1){
var a = str1.charAt(str1.indexOf('[')+1);
var b = str1.charAt(str1.indexOf(']')-1);
return [a, b];
//or
//a.concat(b); //to get a string with that values
}
console.log(extract(str1));

Regex match newline in textarea

I have to get the value from a textarea using jQuery and count the number of newlines there. I'd like to do this using a regex-expression. Does anyone know how to do that?
regex does not have count. better use array like this
var val = textarea.value;
var arr = val.split(/[\n\r]/g);
var count = arr.length;
you could condense this in less rows and vars...
var count = $('textarea').val().split(/[\n\r]/g).length;
http://jsfiddle.net/qxKmW/1/
$(document).ready(function() {
var str = $("#txtField").val();
parts = str.split(/[\n\r]/g);
var newline_count = parts.length;
alert("Count: " + newline_count);
});

Categories