JavaScript remove leading and trailing spaces [duplicate] - javascript

This question already has answers here:
Trim string in JavaScript
(20 answers)
Closed 9 years ago.
Here is a test:
console.log(" Golden Katana of the Unflinching Dawn ".replace(/\s+/g,""))
I want to remove the extra spaces at the end of the string but it removes every space in the string, so how could I remove just the extra spaces at the end and just keep Golden Katana of the Unflinching Dawn?

You can use str.trim() for this case. It removes the leading and trailing whitespace from a string. The regular expression str.replace(/^\s+|\s+$/g,'') will alternatively do the same thing.

try doing
trimmedstr = str.replace(/\s+$/, '');
or maybe
.replace(/ +$/, "");

Related

Replace Space with Dash and Remove Semicolon [duplicate]

This question already has answers here:
Replace comma or whitespace with hyphen in same string
(3 answers)
Closed 2 years ago.
This regex replaces empty spaces with a dash replace(/\s+/g, "-").
If I also want to remove any semicolons, how do I go about adding that to the above regex?
Basically, I want this string hello; how are you to be hello-how-are-you
You could add ; in [] - which means groups and ranges
/[\s;]+/g
const str = "hello; how are you"
const res = str.replace(/[\s;]+/g, "-")
console.log(res)

Removing all occurrences of '^' from a String [duplicate]

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 2 years ago.
I have this sorta string = "^My Name Is Robert.^"
I want to remove the occurrences of ^ from this string. I tried the replace method like :
replyText.replace(/^/g, '');
But it hasn't any affect. Using the replace without the global works but only removes the first occurrence.
Should I just make a loop and keep looping the string with replace till no more '^' are contained, or is there a better way?
You need to escape the ^ character in RegEx:
replyText.replace(/\^/g, '');
The caret, ^, is a special character in Regex, therefore it has to be escaped with a backslash i.e
replyText.replace(/\^/g, '')

Multiple conditions in regex - how to replace both colon and space with dash [duplicate]

This question already has answers here:
Replace multiple characters in one replace call
(21 answers)
Closed 3 years ago.
How to replace both colon and space with dash in regex?
Here's what I've managed to do:
to replace space: replace(/\s+/g, '-'),
to replace colon: replace(/:\s+/g, '-').
How do I merge these expressions?
You could do something like this:
var text = "hello: hey"
console.log(text.replace(/(:|\s+)/g, "-"))
Returns "hello--hey"
Use an alternation [ :]
var input = "Hello World:Goodbye";
console.log(input);
input = input.replace(/[ :]+/g, '-');
console.log(input);
Note that this replaces actual spaces, not all whitespace characters, which your original version using \s does.

How to trim whitespace before carriage return in javascript? [duplicate]

This question already has an answer here:
Trim trailing spaces before newlines in a single multi-line string in JavaScript
(1 answer)
Closed 6 years ago.
Let's say I have an input of
123
123 567
987
98765
I know that .replace(/ /g, "").trim() will remove all the white space and leave carriage returns, but I want to leave the whitespace that's in the middle of two character strings contained between 2 carriage returns as well.
Is that possible with regex?
Sounds like you want to match the return with spaces and replace it. So look so whitespace before a return and replace it with just the return/
var str = "hello world \nChees Fries\n\nfoo";
console.log(str.replace(/\s+\n\s*/,"\n"));
Why don't you split using split("\n"); and then trim() each. Also skip out ones with 0 length. Insert the elements into new array and then join("\n").

Split string on newline and comma [duplicate]

This question already has answers here:
How do I split a string with multiple separators in JavaScript?
(25 answers)
Closed 7 years ago.
My input String is like
abc,def,wer,str
Currently its splitting only on comma but in future it will contain both comma and newline.
Current code as below:
$scope.memArray = $scope.memberList.split(",");
In future I need to split on both comma and newline what should be the regex to split both on comma and newline.
I tried - /,\n\ but its not working.
You can use a regex:
var splitted = "a\nb,c,d,e\nf".split(/[\n,]/);
document.write(JSON.stringify(splitted));
Explanation: [...] defines a "character class", which means any character from those in the brackets.
p.s. splitted is grammatically incorrect. Who cares if it's descriptive though?
You could replace all the newlines with a comma before splitting.
$scope.memberList.replace(/\n/g, ",").split(",")
Try
.split(/[\n,]+/)
this regex should work.

Categories

Resources