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]);
}
Related
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 1 year ago.
Improve this question
need to get the output as 1 string instead of looped string
the output I got each letter on its own
need to have the second output which is one word
Thanks in advance:D
let start = 0;
let swappedName = "elZerO";
for (let i = start; i<swappedName.length; i++){
if (swappedName[i] == swappedName[i].toUpperCase()) {
console.log(swappedName[i].toLowerCase());
}else {
console.log(swappedName[i].toUpperCase());
}
}
//Output
E
L
z
E
R
o
// Need to be
"ELzERo"
Use string = string0+string1 , or keep adding values to an array, then join the array with array.join()
MasteringJs has a great guide on ways to merge characters and strings.
let start = 0;
let swappedName = "elZerO";
var outputString="";
var outputStringArray=[];
var newChar="";
for (let i = start; i<swappedName.length; i++){
if (swappedName[i] == swappedName[i].toUpperCase()) {
newChar = swappedName[i].toLowerCase();
}else {
newChar=swappedName[i].toUpperCase();
}
outputStringArray.push(newChar);
outputString+=newChar;
}
console.log("[Output using string1 + string 2] is "+outputString); // Another example of concating string
console.log("[Output using array.join] is "+outputStringArray.join("")); // Another example of concating string
// Need to be
"ELzERo"
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 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;
}
});
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);
}
});
});