This question already has answers here:
How do I make the first letter of a string uppercase in JavaScript?
(96 answers)
Closed 7 years ago.
this is a test. this is one more test. and this also new test.
This is test. This is one more test. And this is new test.
You don't need jQuery for this. You can use Javascript string and array functions.
Split the string by ., to separate different sentences
Trim each sentence
Capitalize first letter
Join by .
var str = 'this is a test. this is one more test. and this also new test.';
var newStr = str.split('.').map(function(el) {
el = el.trim();
return el.substr(0, 1).toUpperCase() + el.substr(1);
}).join('. ');
alert(newStr.trim());
Related
This question already has answers here:
Regex string ends with not working in Javascript
(11 answers)
Closed 1 year ago.
what is way to find if string has new line character(/n) followed by any other character in javascript?
Currently I'm doing this:
if (((mystring).slice(-2)) == "/\n/A") {
//mycode
}
I want to check whether last two characters of string are \n and A.
My thing is not working. Is there any other way to do this?
I want to check whether last two characters of string are \n and A.
You can use String#endsWith
const A = `this is a valid string\nA`;
const B = `this is an invalid string`;
console.log('A', A.endsWith('\nA'));
console.log('B', B.endsWith('\nA'));
This question already has answers here:
Regex matching 5-digit substrings not enclosed with digits
(2 answers)
Closed 2 years ago.
I am creating a function that replaces the string with a
~(number)~.
Now let's say I have a string that says
This is the replacement of ~26~ and ~524~. We still have 2 cadets left. Have2go for the next mission.2
I want to replace all the 2 in a string with ~86~ but when I am doing so the 2 in ~26~ and ~524~ also getting replaced to ~~86~6~ and ```~5~86~4~.
function replaceGameCoordinate() {
var string = `This is the replacement of ~26~ and ~524~. We still have 2 cadets left. Have2go for the next mission.2`
var replaceArr = ['2'];
let patt = new RegExp(`${replaceArr[0]}`, 'gm')
var newString = string.replace(patt, "~86~");
console.log(newString);
}
replaceGameCoordinate();
The expected output should be :
This is the replacement of ~26~ and ~524~. We still have ~86~ cadets left. Have~86~go for the next mission.~86~
So you need a different regex rule. You don't want to replace 2. You want to replace 2 when it's not next to another number or ~.
In order to do this, you can use lookaheads and lookbehinds (although lookbehinds are not yet supported by regexes in JS, I believe, but at least with lookaheads) :
const input = "This is the replacement of ~26~ and ~524~. We still have 2 cadets left. Have2go for the next mission.2";
const regex = /2(?![\d~])/gm // Means : "2 when it's not followed by a digit \d or a ~"
console.log( input.replace(regex, "~86~" ) )
This question already has answers here:
How can I remove a character from a string using JavaScript?
(22 answers)
Closed 5 years ago.
I'm looking on how to remove a char from a string for example let's say i have "#22UP0G0YU" i want it to remove the # from it how would i do? I also have a small little other question too about how to make string upper case as well thanks in advance.
To remove a specific char I normally use replace, also good for a set of chars:
var str = '#22UP0G0YU';
var newString = str.replace('#', ''); // result: '22UP0G0YU'
To Uppercase, just use .toUpperCase();
var str = '#22UP0G0yu';
var newString = str.replace('#', '').toUpperCase(); // result: '22UP0G0YU'
This question already has answers here:
Replace a Regex capture group with uppercase in Javascript
(7 answers)
Closed 5 years ago.
I wonder if there is a way to make a string uppercase using regex only in JS.
The thing is that I am giving my users a string transformation system.
The user supply me with three parameters : original text, replace regex, subtitution regex.
for example:
original : 'stackoverflow'
replace : /([a-z])(.*)/g
subtitution : $1
Result : 's'
I want to give them the abilitty to set the entire string to uppercase. I've noticed in some other SO questions that there are systems that allows that. for example in sublime text you can do '/\U$1/' to set the entire string to uppercase.
Notice: I cannot use toUpperCase or toLowerCase in any way
Javascript has an inbuilt uppercasing method
var str = "Hello World!";
var res = str.toUpperCase();
The result of res will be:
HELLO WORLD!
This question already has answers here:
How do I make the first letter of a string uppercase in JavaScript?
(96 answers)
Closed 8 years ago.
Now I have this regex:
tSzoveg = tSzoveg.replace(/\b[A-Z]{2,}\b/,'');
It is almost that I want. I want to convert this: THIS IS MINE, NOT YOURS. to this: This is mine, not yours.
How can I convert it to a normal sentence?
function capitalize(string)
{
return string.toUpperCase().charAt(0) + string.toLowerCase().slice(1);
}
I would use toLowerCase() and then modify the first letter:
var str = "THIS IS MINE, NOT YOURS."
str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();