Trim name in javascript [duplicate] - javascript

This question already has answers here:
Remove ALL white spaces from text
(14 answers)
Closed 4 years ago.
var name="james mark";
var name="james " ;
so how to trim both the names occur any condition from this anytime in javascript
Output:
james mark
james

You can replace all instances of repeated white space with a single space and also trim all leading and trailing white space:
name.replace(/\s+/g,' ');
name.replace(/^\s+|\s+$/g,''); // or use .trim()
for more information you should read the description on the .replace() and .trim() functions on MDN

Try with:
var name="james mark";
var j = names.split(' ');
alert(j[0]);
//The otput: James (without the blank spaces)

Related

Extract text from enclosing parentheses using JavaScript [duplicate]

This question already has answers here:
Get text between two rounded brackets
(7 answers)
Closed 2 years ago.
I have a string like Manila (Philippines) and want to replace it with only the substring Philippines. I tried using the following regex pattern, which works in Notepad++:
[^\(]+ \(([^\)]+)\)
However, I get an undefined result in JavaScript:
var x = "Manila (Philippines)";
console.log(x.replace(/[^\(]+ \(([^\)]+)\)/,$1));
You just forgot the " around your replace pattern!
console.log(x.replace(/[^\(]+ \(([^\)]+)\)/,"$1")); will work correctly!
You can use .match():
var x = "Manila (Philippines)";
var result = x.match(/\((.+)\)/).pop();
// regex for string contained in parentheses
console.log(result);

find and replace '%20' with a space in a string javascript [duplicate]

This question already has answers here:
Javascript replace all "%20" with a space
(7 answers)
Closed 4 years ago.
I'm having some trouble trying to figure this out,
basically I have a url string like so this%20is%20a%20string now what I want to do is find and replace all instances of %20 and replace with a space so the string then becomes this is a string.
Now I've tried to do something like this..
if(string.includes('%20')) {
const arr = str.split('%20');
}
which splits the string into an array, but I'm not sure how I can then turn the array of seperate strings into a full string with spaces between each word.
Any help would be appreciated.
Using regex,
str.replace(/%20/g, ' ');
Just use join:
str.split('%20').join(" ")
let val = "this%20is%20a%20string".replace(/%20/g, ' ');
alert(val);
replace

How can I remove all whitespace and carriage returns from a string? [duplicate]

This question already has answers here:
Remove ALL white spaces from text
(14 answers)
Closed 4 years ago.
I have string which is returning this to me:
<div class=\"sqs-layout sqs-grid-12 columns-12\" data-layout-label=\"Post Body\" data-type=\"item\" data-updated-on=\"1539277027193\" id=\"item-5bbf7e3f9140b70d3962e2a0\"><div class=\"row sqs-row\"><div class=\"col sqs-col-12 span-12\"><div class=\"sqs-block image-block sqs-block-image\" data-block-type=\"5\" id=\"block-yui_3_17_2_1_1539794362468_25479\"><div class=\"sqs-block-content\">\n\n \n\n \t\n <div class=\"image-block-outer-wrapper">
Any idea how I can remove all the whitespaces and put this into a JS variable? I have tried countless variations of this:
.replace(/[\n\t\r]/g,"")
but nothing working yet...
You have to assign the value after the replace to another variable or the same variable. This is because strings are immutable. Also add \s on the regex pattern.
var a = "\n\n \n\n \t\n "
var b = a.replace(/[\n\t\r\s]/g, '');
//now b = "" and a still is "\n\n \n\n \t\n "
a = a.replace(/[\n\t\r\s]/g, ''); // this also work

JavaScript how to strip/remove a char from string [duplicate]

This question already has answers here:
How can I remove a character from a string using JavaScript?
(22 answers)
Closed 5 years ago.
I'm looking on how to remove a char from a string for example let's say i have "#22UP0G0YU" i want it to remove the # from it how would i do? I also have a small little other question too about how to make string upper case as well thanks in advance.
To remove a specific char I normally use replace, also good for a set of chars:
var str = '#22UP0G0YU';
var newString = str.replace('#', ''); // result: '22UP0G0YU'
To Uppercase, just use .toUpperCase();
var str = '#22UP0G0yu';
var newString = str.replace('#', '').toUpperCase(); // result: '22UP0G0YU'

Split string by space javascript [duplicate]

This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 7 years ago.
I want to split a string in Java script using
str.split([separator[, limit]])
I want to split it by a space, but ' ' did not work.
What should I use?
This question has previously been asked here: How do I split a string, breaking at a particular character?
In your specific case, based on what you provided, it appears you are attempting to split on nothing. '' instead of an actual space ' '.
To replace the spaces by commas:
var str = "How are you doing today?";
var res = str.split(" ");
//Output: How,are,you,doing,today?
Like described here: http://www.w3schools.com/jsref/jsref_split.asp
Another option is to use str.replace:
Var str = "Mr Blue has a blue house and a blue car";
var res = str.replace(/blue/gi, "red");
//Output: Mr red has a red house and a red car
Like described here: http://www.w3schools.com/jsref/jsref_replace.asp
But what you may actually wanted:
var str = "Test[separator[, limit]]Test";
var res = str.split("[separator[, limit]]").join(" ");
//Output: Test Test

Categories

Resources