This question already has an answer here:
use regex to replace all but first occurrence of a substring of blanks
(1 answer)
Closed 9 years ago.
How can I make a Regex which replaces all occurrences of a word but the first?
I have a webpage with loads of text and a header at the top. I want to make a regex which replaces all occurrences of a word but the first because I don't want the header to change.
var count = 0;
text = text.replace(myRegex, function(match) {
count++;
if(count==1) {
return match;
}
else {
return myReplacedValue;
}
});
You could do this:
var i = 0;
"foo foo foo".replace(/foo/g, function(captured/*, offset, originalString */) {
if ( i++ ) {
return 'bar';
}
return captured;
});
great answer abstract... i have never seen replace used like that before
heres a nice cluttered version..
text = text.replace(/boo/g, function(match) {return (++count==1)?match:myReplacedValue});
Related
This question already has answers here:
Convert string to Title Case with JavaScript
(68 answers)
How do I make the first letter of a string uppercase in JavaScript?
(96 answers)
How can I capitalize the first letter of each word in a string using JavaScript?
(46 answers)
Closed 4 years ago.
I basically want to capitalize the first letter in every word in a sentence, assuming that str is all lowercase. So here, I tried to split the string, letter by letter, then by using for loop, I would capitalize whatever the letter that's after a space. Here's my code and could you please point out where I coded wrong? Thank you.
function titleCase(str) {
var strArray = str.split('');
strArray[0].toUpperCase();
for (i=0; i<strArray.length;i++){
if (strArray[i]===" "){
strArray[i+1].toUpperCase();
}
}
return strArray.join('');
}
You need to assign the values:
function titleCase(str) {
var strArray = str.split('');
strArray[0] = strArray[0].toUpperCase();
for (i=0; i<strArray.length;i++){
if (strArray[i]===" "){
strArray[i+1] = strArray[i+1].toUpperCase();
}
}
return strArray.join('');
}
You can try following
function titleCase(str) {
var strArray = str.split(' ');
for (i=0; i<strArray.length;i++){
strArray[i] = strArray[i].charAt(0).toUpperCase() + strArray[i].slice(1);
}
return strArray.join(' ');
}
console.log(titleCase("i am a sentence"));
This question already has answers here:
Find and replace nth occurrence of [bracketed] expression in string
(4 answers)
Closed 5 years ago.
This question been asked before, but I did not succeed in solving the problem.
I have a string that contains numbers, e.g.
var stringWithNumbers = "bla_3_bla_14_bla_5";
I want to replace the nth occurence of a number (e.g. the 2nd) with javascript. I did not get farer than
var regex = new RegExp("([0-9]+)");
var replacement = "xy";
var changedString = stringWithNumbers.replace(regex, replacement);
This only changes the first number.
It was suggested to use back references like $1, but this did not help me.
The result should, for example, be
"bla_3_bla_xy_bla_5" //changed 2nd occurence
You may define a regex that matches all occurrences and pass a callback method as the second argument to the replace method and add some custom logic there:
var mystr = 'bla_3_bla_14_bla_5';
function replaceOccurrence(string, regex, n, replace) {
var i = 0;
return string.replace(regex, function(match) {
i+=1;
if(i===n) return replace;
return match;
});
}
console.log(
replaceOccurrence(mystr, /\d+/g, 2, 'NUM')
)
Here, replaceOccurrence(mystr, /\d+/g, 2, 'NUM') takes mystr, searches for all digit sequences with /\d+/g and when it comes to the second occurrence, it replaces with a NUM substring.
var stringWithNumbers = "bla_3_bla_14_bla_5";
var n = 1;
var changedString = stringWithNumbers.replace(/[0-9]+/g,v => n++ == 2 ? "xy" : v);
console.log(changedString);
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.
function to capitalise first letter of a string - 'toUpperCase' , underscore and other jQuery are excluded . I reworked a vers with underscore which I can't use
```
function capitalize (str){
var str = "";
var lowercase = "";
var Uppercase = "";
str.forEach(){
for (i=0; i < str.length; i++);
}
return Uppercase[lowercase.indexOf(str0)];
}
```
There are lots of reduced vers using toUpperCase
Any links, code help pls .... Tks
The best method I've found is just to call toUpperCase on the first character and concat the rest of the string using slice:
function capitalize(str) {
if(typeof str === 'string') {
return str[0].toUpperCase() + str.slice(1);
}
return str;
}
If you want to capitalize each word in a sentence, you can split on space:
"capitalize each word of this sentence".split(' ').map(capitalize).join(' ');
This question already has answers here:
Capitalize words in string [duplicate]
(21 answers)
Closed 7 years ago.
What to do - Capitalize the first letter of the words in a sentence.
So, I solved it and was wondering is there any way to do it without making it an array with .split().
What I tried without turning it into a array -
The logic - First, turn everything into lowercase. Then scan the sentence with a for loop, if you find a space, capitalize the next character.
function titleCase(str) {
str = str.toLowerCase();
for(i=0;i<str.length;i++) {
if(str[i]===" ") {
str = str.charAt[i+1].toUpperCase();
return str;
}
}
}
titleCase("I'm a little tea pot", "");
That code doesn't even run.
I just used split() and replace to do that. You can have a look at my code.
function titleCase (str)
{
str = str.split(' ');
for(var i=0;i<str.length;i++)
{
str[i] = str[i].replace(str[i][0],str[i][0].toUpperCase())
}
return str.join(' ');
}
var mainString ="i am strong!";
titleCase(mainString);
Here is one using replace + with a regex:
/**
* #summary Uppercase the first letter in a string.
* #returns {string}
*/
function uppercaseFirstLetters(string) {
return string.replace(/[a-zA-Z]*/g, function(match) {
return match.charAt(0).toUpperCase() + match.substr(1).toLowerCase();
})
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Fastest method to replace all instances of a character in a string
How can you replace all occurrences found in a string?
If you want to replace all the newline characters (\n) in a string..
This will only replace the first occurrence of newline
str.replace(/\\n/, '<br />');
I cant figure out how to do the trick?
Use the global flag.
str.replace(/\n/g, '<br />');
Brighams answer uses literal regexp.
Solution with a Regex object.
var regex = new RegExp('\n', 'g');
text = text.replace(regex, '<br />');
TRY IT HERE : JSFiddle Working Example
As explained here, you can use:
function replaceall(str,replace,with_this)
{
var str_hasil ="";
var temp;
for(var i=0;i<str.length;i++) // not need to be equal. it causes the last change: undefined..
{
if (str[i] == replace)
{
temp = with_this;
}
else
{
temp = str[i];
}
str_hasil += temp;
}
return str_hasil;
}
... which you can then call using:
var str = "50.000.000";
alert(replaceall(str,'.',''));
The function will alert "50000000"