How to cut dot in string? [duplicate] - javascript

This question already has answers here:
Javascript string replace not working [duplicate]
(3 answers)
Closed 6 years ago.
I tried to cut dot from string using the following function:
function removeSymbols(str){
console.log(str.length);
str.replace(/\./g, "");
return str;
}
var str = " народу.";
But it does not cut

Change your return statement from
return str;
To
return str.replace(/\./g, "");

function removeSymbols(str) {
console.log(str.length);
str = str.replace(/\./g, "");
return str;
}
var str = " народу.";
console.log(removeSymbols(str));

replace doesn't change the original string, it will return a new string that is replaced.

Related

How to increment only digits of string? [duplicate]

This question already has answers here:
How to increment number in string using Javascript or Jquery
(6 answers)
Closed 6 years ago.
I may have following type strings
A1 or 1A or AB....1 or 1AB......
so how to increment only digits of above type of strings in javascript?
var adminNo = data.Admission_No.slice(-2);
alert(adminNo);
var removedNo = data.Admission_No.substring(data.Admission_No.length-1);
alert(removedNo);
Use the replace method as shown in demo below
function incrementer(input)
{
return input.replace(/\d+/, function(match){ return parseInt(match) + 1 });
}
alert(incrementer("A1"));
alert(incrementer("1A"));
This will find the integer anywhere in the input string and increment it by one.
string.replace(/\d+/, function(n){ return ++n });
You can do it by taking out integer from your string
Its big long, but more self-explainatory
var youroriginalstring="A1"
var withNoDigits = youroriginalstring.replace(/[0-9]/g, '');
var yournumber = youroriginalstring.replace ( /[^\d.]/g, '' );
var incNos=yournumber +1;
var newString = incnos + "withNoDigits"

Basic Javascript Algorithm [duplicate]

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();
})
}

Get the same function for replace [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 8 years ago.
I have two strings like these
var temp = 'xx-y1 xx-y2 xx-y3';
var temp1 = 'zz-y1 zz-y2 zz-y3';
I wanna replace all the words started with "xx-" and "zz-" pattern and for this purpose I do this.
temp.replace(/\bxx-\S+/g, '');
temp.replace(/\bzz-\S+/g, '');
now my question is how can I have a single function and just call it?
I try to test this but it doesn't work!!!
func = function(str, pattern) {
return str.replace(RegExp('\b' + pattern + '\S+', 'g'), '');
}
You need to escape \ when calling RegExp constructor.
function replace(where, what) {
return where.replace(new RegExp('\\b' + what + '\\S+', 'g'), '');
}

Replacing of string using javascript [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 9 years ago.
I want to replace string from "_tr9-9" character from a string and replace them with _id character
Here str is changes only _tr9-0 will be changes dynamically remining will be same
str=Test_User_tr9-0;
Ex:
function (str)
{
var obj=str
}
In C#
string str = "Test_user_tr9-9";
string str2 = str.Replace("_tr9-9", "_id");
In Javascript
var str = "Test_user_tr9-9";
var str2 = str.replace("_tr9-9", "_id");
Note that both in Javascript and C# strings are immutable objects, so the replace/Replace methods return a new modified string (technically in C# the Replace returns the original string if it doesn't find anything to replace)
In JavaScript:
var index = str.lastIndexOf("_");
var result = str.substring(0, index) + "_id";
jsFiddle: http://jsfiddle.net/fNZkG/
You can use replace
In javascript
var str = "Test_user_tr9-9 to Test_user_id";
str = str.replace('tr9-9','id');
In C#
var str = "Test_user_tr9-9";
var str = str.Replace("_tr9-9", "_id");

replace all occurrences in a string [duplicate]

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"

Categories

Resources