Javascript Replace the first character [duplicate] - javascript

This question already has answers here:
How do I replace a character at a particular index in JavaScript?
(30 answers)
Closed 1 year ago.
How do I replace the first character of a string with another character? I am casting the number as string.
Example: String 12341
New string: 92341
I tried this script but cant figure a way to only replace the first character.
var oldStr =12341;
var newStr = oldStr.replace(1, 9);

oldStr is not a string, it's a number.
replace has to replace characters, not numbers.
So coerce the number to a string, and then do the replacement.
const oldStr = 12341;
const newStr = oldStr.toString().replace('1', '9');
console.log(newStr);

Related

Regex to format names [duplicate]

This question already has answers here:
Regex expression not working with once or none
(2 answers)
Closed 3 years ago.
Looking to find a regex that can remove characters and replace them with either a space, or with a comma and a space.
The problem is it ends up having 3 spaces, when I just want one. And if I try doing ", " I get 3 commas.
An example of how I want the output to be:
FirstName LastName, FirstName Lastname
var str = "&q=FirstNameLastName&q=FirstName2LastName2"
var newStr = str.replace(/[&q=]/g, ' ');
console.log(newStr)
Get rid of the square brackets. [&q=] matches any single character that's either &, q, or =, and then you replace each of them with a space, so you get 3 spaces. This will also replace q characters in the names.
Just write the string that you want to replace by itself.
var str = "&q=FirstNameLastName&q=FirstName2LastName2"
var newStr = str.replace(/&q=/g, ' ');
console.log(newStr)

Remove First Character of (Original) String in JavaScript [duplicate]

This question already has answers here:
Delete first character of string if it is 0
(17 answers)
the best way to remove the first char of a given string in javascript
(4 answers)
Closed 3 years ago.
I see this article but it's specific to deleting a character if it's a certain number (0, in that case).
I want to remove the first character from a string no matter what it is.
splice() and shift() won't work because they're specific to arrays:
let string = "stake";
string.splice(0, 1);
console.log(string);
let string = "stake";
string.shift();
console.log(string);
slice() gets the first character but it doesn't remove it from the original string.
let string = "stake";
string.slice(0, 2);
console.log(string);
Is there any other method out there that will remove the first element from a string?
Use substring
let str = "stake";
str = str.substring(1);
console.log(str);

Why does replace('_', '-') not work in the case of a double underscore? [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 4 years ago.
I'm curious about the following code and why it is that replace is not replacing the second underscore in string2. It was my assumption that replace is going character by character through the string looking to replace the given value with the new value, but that apparently is not exactly the case.
const string1 = "hello_world"
const string2 = "hello__world"
string1.replace('_', '-') // 'hello-world'
string2.replace('_', '-') // 'hello-_world'
In order for it to work as intended you have to use a regex to match all the underscores in your string like so
const string1 = "hello_world"
const string2 = "hello__world"
string2.replace('_', '-') // 'hello-_world'
string2.replace(/_+/g, '-') // 'hello-world'
You will need to use a regex modifier, in this case g. This replaces all instances of the pattern.
const string1 = "hello_world"
const string2 = "hello__world"
console.log(string1.replace(/_/g, '-'))
console.log(string2.replace(/_/g, '-'))
Here's an answer for your question.
.replace() only replace the first character. You can know more about replace() here

How to get the index of the backslash (\) in a string in javascript? [duplicate]

This question already has answers here:
Javascript - Replacing the escape character in a string literal
(4 answers)
Closed 5 years ago.
I have a string like this :
var name="C:\Users\Desktop\task.txt";
I want to get the last index of backslash in the above string.
How to do it ?
There are no backslashes in that string.
The backslashes in the source code of the string literal will be treated as escape characters and consumed by the time the string is constructed.
\U becomes U, \D becomes D, and \t becomes a tab.
var name="C:\Users\Desktop\task.txt";
console.log(name);
If there were any backslashes in the string, then you could use lastIndexOf (keeping in mind that you would need to escape the backslash in the needle as well as the haystack).
var name = "C:\\Users\\Desktop\\task.txt";
console.log(name);
console.log(name.lastIndexOf("\\"));
I got the filename alone by
var filename=document.getElementById('file').value;
var lastIndex = filename.lastIndexOf("\\");
if (lastIndex >= 0) {
originalfilename = filename.substring(lastIndex +1);
}

How can I replace only plus sign (multiple occurrence) from a string without any text [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Replace all plus signs (+) with space in a string
(1 answer)
Closed 5 years ago.
How can I replace multiple (++++++) signs with blank space?
Example: var string = "+++++++++++++++++";
var string = "+++++++++++++++++";
string = string.replace(/\+*/g, '');
console.log(string.length);
const str = 'abc++++++def++++frberbf++fsvsf';
const newstr = str.replace(/(.)\+{2,}/g, ' ');
console.log(newstr);

Categories

Resources