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)
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
How to Write a function for converting any Captalize String into small letter dashed String and Vice Versa.
1st Example
TechnologyStackPage --> technology-stack-page
and
technology-stack-page --> TechnologyStackPage
2nd Example
EHSCustomisedSoftwareEstimationForm --> ehs-customised-software-estimation-form
and
ehs-customised-software-estimation-form --> EHSCustomisedSoftwareEstimationForm
const abbr = ['EHS', 'USA']
const kebabCase = new RegExp(`(?=(?<=${abbr.join('|')}|[a-z])[A-Z]+)`, 'g')
const pascalCase = new RegExp(`(^|-)(${abbr.join('|').toLowerCase()}|[a-z])`, 'g')
console.log(``,
'EHSCustomisedSoftwareEstimationForm'.replace(kebabCase, `-`).toLowerCase(),`\n`,
'CustomisedSoftwareEstimationFormUSA'.replace(kebabCase, `-`).toLowerCase(),`\n`,
`\n`,
'ehs-customised-software-estimation-form'.replace(pascalCase, (_, _1, _2) => _2.toUpperCase()),`\n`,
'customised-software-estimation-form-usa'.replace(pascalCase, (_, _1, _2) => _2.toUpperCase())
)
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
i want to sort firstname and lastname, my problem is when the user has multiple first name, e.g.:
'Michael Jordan Atticus Smith'
basically, the Smith is the last name there, how do i split firstname and lastname with it? the result i want from is:
'Michael Jordan Atticus' 'Smith'
let str = 'Michael Jordan Atticus Smith';
console.log( ex1(str) );
console.log( ex2(str) );
console.log( ex3(str) );
function ex1(str) {
let names = str.match(/(.*?)\s(\S+)$/); // (1)
return [ names[1], names[2] ];
}
function ex2(str) {
let names = str.split(" ");
return [names.pop(), names.join(" ")].reverse(); // (2)
}
function ex3(str) {
let last = ""
str = str.replace(/\s(\S+)$/, function(full_match, parenthesis_1) {
last = parenthesis_1;
return "";
});
return [str, last];
}
(1) match() returns an array of matched strings. Also, see the RegEx cheet sheet
(2) Array pop() method removes, and returns the last element. Code executes from the left to right, so, the second names.join(" ") collects together 3 names left.
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 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);
}
});
});