I have this long string and I want a part of it transformed into white colour using only JavaScript.
Example 1:
var string = document.getElementById("subtitle").innerHTML; //returns the string
var i = string.indexOf("("); //returns 80
var j = string.indexOf(")"); //return 93
Example 2: I can get the wanted text but I don't know how to change it white
var string = document.getElementById("subtitle").innerHTML; //returns the string
var i = string.indexOf("(");
var j = string.substring(i, string.indexOf(")")+1); //return the exact string I want to paint white
//j.paintWhite(); how?
I would like to paint all the characters between positions 80 and 93 (or selected as shown in example #2) white. How can I do it?
You need to create an html container for that text where you can specify a style attribute.
also, do not use the variable name string as it is reserved to the language
In order to do that, I would recommend using jQuery, as it is a bit easier.
But if you don't want to, you can do:
var text = document.getElementById("subtitle").innerHTML;
var cut = text.split("(");
var cut2 = cut[1].split(")");
var colored = cut[0] + '<span style="color:#fff;">('+cut2[0]+')</span>'+cut2[1];
document.getElementById("subtitle").innerHTML = colored;
if you assume "transformed into white colour" is doing
<span style="color:#fff">MY_TEXT_HERE</span>
then you could try the following with arrays:
var string = document.getElementById("subtitle").innerHTML;
var arr1 = string.split('(');
var arr2 = arr1[1].split(')');
var finalString = arr1[0] + '<span style="color:#fff">' + arr2[0] + '</span>' + arr2[1];
You must modify the inner Html of the element so it has a text element nested with a defind style.
http://jsfiddle.net/gsexxsbs/
var element = document.getElementById("subtitle");
var htmlText = element.innerHTML;
var i = htmlText.indexOf("(");
var j = htmlText.indexOf(")")+1;
var redText = htmlText.substring(i, j);
element.innerHTML = htmlText.substring(0,i)+"<a style='color:red;'>"+redText+"</a>"+htmlText.substring(j);
Related
var str = '<h2 id="test1">11</h2><h2 id="test2">22</h2>'
var arr1 = ["test1","test2"]
var arr2 = ["11","22"]
How can i get arr1/arr2 from str?
In real life, elements of your interest may be interleaved with other
elements, having also e.g. id attributes.
So the regex to get the text after id=" up to the next " may be not
enough.
The situation is even worse, when you want to retrieve the text content of
the element, especially if it contains its own child elements.
So in the case of HTML it is generally much easier and more natural to use
DOM methods and attributes, instead of regex.
You can e.g. create a DOM element and set its inner HTML (from your string).
Then, you can retrieve its child elements, e.g. by tag name and process them in
a loop.
Then (in the loop), having each current element, you can get either any its
attribute or inner text and push it into a respective array (created before).
So the example Javascript fragment, demonstrating the methods to use, can look like below:
<script>
var str = '<h2 id="test1">11</h2><h2 id="test2">22</h2>';
var el = document.createElement('div');
el.innerHTML = str;
var elems = el.getElementsByTagName('h2');
var arr1 = [], arr2 = [];
for (var i = 0; i < elems.length; i++) {
var currElem = elems[i];
arr1.push(currElem.getAttribute("id"));
arr2.push(currElem.innerText);
}
document.write('arr1: ' + arr1);
document.write('<br/>');
document.write('arr2: ' + arr2);
</script>
Of course, your final goal is not to write the arrays to the document, but make
of them any use you intend, so I wrote document.write only for demonstration purpose.
get your ids using the following regex
var str = '<h2 id="123"></h2><h2 id="123"></h2>';
var res = str.match(/(id="(.*?)(\"))/g);
You can use this code, it will return exact what you want.
<script type="text/javascript">
var str1 = '<h2 id="test1">11</h2><h2 id="test2">22</h2>';
var str2 = '<h2 id="test1">11</h2><h2 id="test2">22</h2>';
var pattern_id = /id=\"([a-zA-Z0-9-]+)\"/;
var pattern_value = /\>(\d+)\</;
var id_array = [];
var value_array = [];
do {
var array = str1.match(pattern_id);
if(array != null)
{
id_array.push(array[1]);
str1 = str1.replace(pattern_id, '');
}
}while(array != null);
do {
var array = str2.match(pattern_value);
if(array != null)
{
value_array.push(array[1]);
str2 = str2.replace(pattern_value, '');
}
}while(array != null);
console.log(id_array); // id are stored here ex: test1, test2
console.log(value_array); // value are stored here, 11, 22
</script>
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.
Instead of "var instance = ..." adding the two values it concatenates them. Can anyone suggest what I need to fix?
I'm trying to add "var startingEmail" value and "var k".
Thank you for your help!
var startingEmail = sheet.getRange("C2").getDisplayValue();
var numEmails = sheet.getRange("E2").getDisplayValue();
var max = numEmails;
for (var k = 0; k<max; ++k){
var threads = GmailApp.getInboxThreads(startingEmail,max)[k]; //get max 50 threads starting at most recent thread
var messages = threads.getMessages()[0];
var sndr;
var rcpnt;
var srAry = [];
var sndr = messages.getFrom().replace(/^.+<([^>]+)>$/, "$1"); //http://stackoverflow.com/questions/26242591/is-there-a-way-to-get-the-specific-email-address-from-a-gmail-message-object-in
var sndrLower = sndr.toLowerCase;
var rcpnt = messages.getTo().replace(/^.+<([^>]+)>$/, "$1");
var rcpntLower = rcpnt.toLowerCase;
var cc = messages.getCc().replace(/^.+<([^>]+)>$/, "$1");
var ccLower = cc.toLowerCase;
//srAry.push(sndr);
//srAry.push(rcpnt);
//srAry.push(cc);
var isIn = joinAddr.search(sndr || rcpnt);
if(isIn == -1){
var instance = k;
I can't see the example in your code but it sounds like you can just wrap Number() around your variable and it will perform the type conversion so the code will perform the math instead of concatenating as strings.
I've created an associative array for index spaces inside a sentence for example:
sentence: hello how are you? (spaces between the word 'hello' to 'how')
so my array looks like this:
indexed_words[0] = hello
indexed_words[0_1] = space
indexed_words[0_2] = space
indexed_words[0_3] = space
indexed_words[0_4] = space
indexed_words[0_5] = space
indexed_words[0_6] = space
indexed_words[0_7] = space
indexed_words[1] = how
indexed_words[2] = are
indexed_words[3] = you?
but when I use 'for' loop its show me (using alert) the indexes 0,1,2,3 first and after them the sub-indexes, its mixed up my array order, any idea?
here my code:
function words_indexer(user_content)
{
var words_array = user_content.split(" ");
var indexed_words = {};
var word_counter = 0
var last_word_counter = 0
$.each(user_content, function(word_key,word_value){
if(word_value === ''){
var indexed_key = last_word_counter + '_' + word_key;
indexed_words[indexed_key] = word_value;
}else{
var indexed_key = word_counter;
indexed_words[indexed_key] = word_value;
last_word_counter = word_counter;
word_counter++;
}
});
for (var key in indexed_words) {
alert(key + ' ' + indexed_words[key]);
}
}
If your array index needs an extra level of structure then it may be better to just create a nested array instead:
indexed_words[0] = hello
indexed_words[0][1] = space
indexed_words[0][2] = space
indexed_words[0][3] = space
indexed_words[0][4] = space
indexed_words[0][5] = space
indexed_words[0][6] = space
indexed_words[0][7] = space
indexed_words[1] = how
indexed_words[2] = are
indexed_words[3] = you?
I believe adding an underscore to your array key may actually cause Javascript to consider it as being a string which would bump your numeric keys up above it.
You can't use non-numeric indexes for arrays in javascript (a_b is not considered numeric). For this you probably should use an object. And then loop through it like this:
for(var word_key in indexed_words) {
if(!indexed_words.hasOwnProperty(word_key)) continue;
var word_value = indexed_words[word_key];
// Your code
}
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);
});