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

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

Related

Javascript Replace the first character [duplicate]

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

Split by word not contained in parentheses [duplicate]

This question already has answers here:
How to split by commas that are not within parentheses?
(6 answers)
How to split string while ignoring portion in parentheses?
(5 answers)
Closed 3 years ago.
Here is the my string
var string = 'Title:(India OR America) OR Keyword:(Education)';
After splitting with "OR" it is showing
["Title:(India", "America)", "Keyword:(Education)"]
But what I need is the below one.
["Title:(India OR America)", "Keyword:(Education)"]
Could anyone please help on how to split the string to get the required result.
To achieve this you'll need to use a negative lookahead to only find the OR string where it's not contained in parentheses. Try this:
var input = 'Title:(India OR America) OR Keyword:(Education)';
var output = input.split(/(?!\(.*)\s?OR\s?(?![^(]*?\))/g);
console.log(output);

How to cut off the last newline from Javascript string containing multiple lines [duplicate]

This question already has answers here:
Trim specific character from a string
(22 answers)
Closed 3 years ago.
In Javascript, I have a generated string containing multiple lines, all ending in a newline, for example: "line1\nline2\nline3\n"
What is the best way to cut off the ending newline such that I get "line1\nline2\nline3" ?
For safety, it would be preferrable if the last character is only cut off if it actually is a newline. So if my input is "field1=5\nfield2=abc\nfield3= some string \n" I want to remove the last newline but keep the two spaces.
There are lots of general-purpose solutions out there, but JavaScript itself doesn't really contain a built-in specifically for removing a particular character. The replace() method combined with a regular expression, however, still does the trick just fine:
var string = "field1=5\nfield2=abc\nfield3= some string \n";
var trimmed = string.replace(/\n+$/, '');
Try:
var str = "line1\nline2\nline3\n"
if (str[str.length - 1] === '\n') str = str.substring(0, str.length - 1);
console.log(str);

Replacing a string if it contains alphabet from an array in js [duplicate]

This question already has answers here:
Strip all non-numeric characters from string in JavaScript
(12 answers)
Closed 3 years ago.
I have an array like ['adsd','ssd2','3244']. I want to replace a string if it contains any alphabet with '----'. So, the above array should be like ['----','----','3244']. How can I do that? Can I so it with regular expression?
yourArray.map(str => /[a-z]/i.test(str) ? '----' : str)
['adsd','ssd2','3244'].map(function(item) {
return /[a-z]/i.test(item) ? '----' : item;
});
Edit: a bit of explanation about what's happening here.
map() applies a function that transforms every element of the array. More info.
/[a-z]/i is a regex that matches every character in the alphabet. The i makes it case-insensitive, so it matches a and also A.
test checks whether a given string matches the regex. More info.
? '----' : item uses a ternary operator to return either ---- or the original string, depending on whether the string has any alphabetic character. More info.

replace all occurence of a string with a character using javascript [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 8 years ago.
I wan to replace all occurrence of a string with single quote but with str.replace it only replaces the first occurrence of the script:
"7<singleQuote>1 inche<singleQuote>s"
Code
var data = "7<singleQuote>1 inche<singleQuote>s"
var output = data.replace("<singleQuote>","'")
Output: 7'1 inche<singleQuote>s
I want to replace <singleQuote> with '.
Use regex with g flag:
var output = data.replace(/<singleQuote>/g, "'");
MDN: String.prototype.replace.

Categories

Resources