Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Using JavaScript, how to find every element that have font-style: italic (in addition to <i> and <em>), and switch it to font-style: normal if the element contains one of more characters that are not Latin characters ([a-zA-Z])?
$('selector_for_text_containers').each(function(){
var str = $(this).attr('style').replace('italic', 'normal');
$(this).attr('style', str);
});
You can use the method given from #Loyalty Technology in this function to test if the chars are available.
function validate() {
var chars = 'άλφα';
$.each( $('.text') , function (indx, elm) {
var text = $(elm).text().split('');
text.forEach( function( letter, ind ) {
if ( chars.indexOf(letter) !== -1) {
var str = $(this).attr('style').replace('italic', 'normal');
$(elm).attr('style', str);
}
});
});
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Can someone help me on this one? Given a string, I have to return a string in which each character (case-sensitive) is repeated once.
doubleChar("String") ==> "SSttrriinngg"
doubleChar("Hello World") ==> "HHeelllloo WWoorrlldd"
doubleChar("1234!_ ") ==> "11223344!!__ "
function doubleChar(str) {
}
You can use repeat() method for this like:
function doubleChar(str) {
return [...str].map(s => s.repeat(2)).join('')
}
console.log(doubleChar("String"))
console.log(doubleChar("Hello World"))
console.log(doubleChar("1234!_ "))
Try this:
const str = 'hello'
let arr = str.split('')
const double = arr.map(i => i += i).join('')
console.log(double)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
i have collections of text with some text between ${ and } like "this is ${test} string ${like}". How can I extract all there strings. Output : test,like
try
match(/{[\w\d]+}/g);
example
"{asdas}32323{234}".match(/{[\w\d]+}/g); //outputs ["{asdas}", "{234}"]
It will return with { and } with the matches which you can remove from the resultset by
"{asdas}32323{234}".match(/{[\w\d]+}/g).map(function(value){return value.substring(1, value.length-1)}); //outputs ["asdas", "234"]
you can try:
"this is ${test} string ${like}".match(/\${\w*}/g).map(function(str){return str.slice(2,-1)})
//["test", "like"]
Try this
var str = "this is ${test} string ${like}";
var txt = str.match(/{[\w\d]+}/g);
for(var i=0; i < txt.length; i++) {
txt[i] = txt[i].replace(/[{}]/g, '');
alert(txt[i]);
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I had a JavaScript interview last wednesday, and I had trouble with one of the questions. Maybe you guys can give me hand with it?
The question was: how would you go about this printing var a and s to the console, in camel case, with the help of a prototype function...
var s = “hello javier”;
var a = “something else”;
String.prototype.toCamelCase = function() {
/* code */
return capitalize(this);
};
...so the result is the same as doing this?
console.log(s.toCamelCase());
console.log(a.toCamelCase());
>HelloJavier
>SomethingElse
Thanks!
var s = 'hello javier';
var a = 'something else';
String.prototype.toCamelCase = function() {
return capitalize(this);
};
function capitalize(string) {
return string.split(' ').map(function(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}).join('');
}
console.log(a.toCamelCase());
console.log(s.toCamelCase());
Reference
How do I make the first letter of a string uppercase in JavaScript?
I would go with something like this:
var s = "hello javier";
var a = "something else";
String.prototype.toCamelCase = function() {
function capitalize(str){
var strSplit = str.split(' ');
// starting the loop at 1 because we don't want
// to capitalize the first letter
for (var i = 1; i < strSplit.length; i+=1){
var item = strSplit[i];
// we take the substring beginning at character 0 (the first one)
// and having a length of one (so JUST the first one)
// and we set that to uppercase.
// Then we concatenate (add on) the substring beginning at
// character 1 (the second character). We don't give it a length
// so we get the rest.
var capitalized = item.substr(0,1).toUpperCase() + item.substr(1);
// then we set the value back into the array.
strSplit[i] = capitalized;
}
return strSplit.join('');
}
return capitalize(this);
};
// added for testing output
console.log(s.toCamelCase());
console.log(a.toCamelCase());
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've a variable called url as follows :
url = "http://56.177.59.250/static/ajax.php?core[ajax]=true&core[call]=prj_name.contactform&width=400&core[security_token]=c7854c13380a26ff009a5cd9e6699840"
Now I want to use if condition only if core[call] is equal to the value it currently has i.e. prj_name.contactform otherwise not.
How should I do this since the parameter from query-string is in array format?
Just use String.indexOf and check if it is present (that is not -1, which means it doesn't exist)
if(url.indexOf("core[call]=prj_name.contactform") > -1){
// valid. Brew some code here
}
You can use location.search :
<script>
function get(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
if(get("core[call]") == "prj_name.contactform"){
alert('ok');
}else{
alert('no');
}
</script>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
$(".p").each(function(i){
len=$(this).text().length;
if(len>80)
{
$(this).text($(this).text().substr(0,80)+'...');
}
});
some of my output is fine like
abc def...
but some of it will be like
1234 45 ...
How to trim the space? I tried $.trim but doesn't work.
This should work:
$(".p").text(function() {
var text = $(this).text();
if (text.length > 80) {
return $.trim(text.substr(0, 80)) + '...';
} else {
return text;
}
});