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;
}
});
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
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 5 years ago.
Improve this question
I have phone number like: 89227611508 and i need to reformat this with regex(JS) into +8 922 761-15-08. I'm new in regex and can't get it done. Can anyone provide a simple solution?
Use:
"89227611508".replace(/^(\d{1})(\d{3})(\d{3})(\d{2})(\d{2})$/, "+$1 $2 $3-$4-$5");
You need to use a regular expression to grab the groups of numbers then concatenate the number groups into a formatted string.
let number = '89227611508';
console.log(formatNumber(number));
function formatNumber(number) {
let groups = number.match(/^(\d)(\d{3})(\d{3})(\d{2})(\d{2})$/);
return '+' + groups[1] + ' ' + groups[2] + ' ' + groups[3] + '-' + groups[4] + '-' + groups[5];
}
.as-console-wrapper { top: 0; max-height: 100% !important; }
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 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);
}
});
});