In my application i want to remove the unwanted space from starting and ending.And also if there have more than space between words also remove them leaving one space.
How can i do this?
Try this:
//remove leading spaces
content = content.replace(/^\s+/g,'');
//remove trailing spaces
content = content.replace(/\s+$/g,'');
//remove whitespaces in the middle
content = content.replace(/\s+/g,' ');
This should work to remove leading and trailing spaces, and reduce two or more spaces to a single space:
str.replace(/^\s+|\s+$/, '').replace('/\s\s+/', ' ')
You can use jQuery trim method.This function remove all new lines and all spaces leading or trailing from input string.
var str = ' it is a test string ';
alert($.trim(str));
Related
I'm trying to generate a link using jQuery and need to trim the last '+' sign off the end. Is there a way to detect if there is one there, and then trim it off?
So far the code removes the word 'hotel' and replaces spaces with '+', I think I just need another replace for the '+' that shows up sometimes but not sure how to be super specific with it.
var nameSearch = name.replace("Hotel", "");
nameSearch = nameSearch.replace(/ /g, "+");
Thanks
You could simply use String.prototype.trim() before you call replace, in order to remove the leading and trailing white-space from the String:
var nameSearch = name.trim().replace("Hotel", "").replace(/ /g, "+");
References:
String.prototype.trim().
You can target the end of the string within a Regex with the $ character. You can remove the + signs from the end like this.
nameSearch = nameSearch.replace(/\+*$/g, "");
But even better, as David Thomas pointed out, you should call trim on the string before your manipulation, so it won't have any leading and trailing white-spaces, hence you won't need to trim the + signs.
Need to escape / in javascript .split() function but cannot seem to figure out!
input -> string "07777/25555,00255" or any of 0777/2555 0777,2555
output -> array {07777,25555,00255}
var p = item.gdp.split(/ , | \//);
Not really good with regex!
What this does is split on either " , " or " /" (note the space characters: space comma space and space forward slash). Your regular expression is absolutely fine if that's what you're intending to replace on.
Here's a Regexper visualisation:
Update
There are no spaces in your string at all, so you need to remove those:
item.gdp.split(/,|\//);
With this, your result will be:
["07777", "25555", "00255"]
A more practical regular expression to use though would be /[,\/] - the square brackets will match on any character held within them.
var item={gdp:"07777/25555,00255"};
var p = item.gdp.split(/[,/]/);
document.write(p[0] + "<br>" + p[1] + "<br>" + p[2]);
07777
25555
00255
Here's one
split(/\s*[,\/]\s*|\s+/);
If you are splitting on only comma and slash as in your first string
"07777/25555,00255"
you can simply split on the character class containing those two characters [,/]
Within a character class the slash does not need to be escaped, so the resulting statement would be
var p = item.gdp.split(/[,/]/);
If you also want to split on space, as in your other example 0777/2555 0777,2555 simply add space to the character class:
var p = item.gdp.split(/[, /]/);
or to split on any whitespace (space, tab, etc.) use the predefined \s:
var p = item.gdp.split(/[,\s/]/);
Further, you can collapse multiple whitespace, but then you need to go beyond a simple character class. Compare...
var str="07777/25555,00255 0777,3444";
// split on white, comma, or slash. multiple spaces causes multiple results
str.split(/[\s,/]/)
// -> ["07777", "25555", "00255", "", "", "", "", "0777", "3444"]
// split on multiple whitespace, OR on comma or slash
str.split(/\s+|[,/]/)
// -> ["07777", "25555", "00255", "0777", "3444"]
input.split(/[\/\s,]+/)
Is this what you are looking for?
I want to trim only regular whitespace and preserve   non-break spaces. What is my best bet?
Update: By trim, I mean either leading or trailing. I specifically need to eliminate leading whitespace other than  , but I will appreciate answers that do the same for also trailing or just trailing.
If the string is the content of an HTML tag you can cheat a bit, using:
var result = $( '#element' ).html().trim();
which will not trim because it will literally be in the string.
If the string is not in a tag you could try:
var result = $( '<p>' + the_string + '</p>' ).html().trim();
which should do the same thing.
Replace all white spaces with blank.
var dest = 'test value';
result_val=dest.split(" ").join("");
demo
If you want to remove empty characters in the middle of the string, you can use this (note the first parameter means any amount of whitespace but shouldn't match an ):
var newString = s.replace(/\s+/g, '');
If not (meaning only remove white space at the start/end of the string), you can use
var newString = s.trim();
I run this regex on #q keyup event to avoid extra spaces in a string.
$('#q').val($('#q').val().replace(/\s+/g,' '));
The problem is that it is also deleting all new lines. How can I delete extra spaces but keep new lines intact?
The issue is that \s represents all whitespace including newlines. If you just want spaces, you can have a literal space:
$('#q').val($('#q').val().replace(/ +/g,' '));
If you want spaces and tabs, you could use a character class instead:
$('#q').val($('#q').val().replace(/[\t ]+/g,' '));
Looking for \x20+ does the trick:
$('#q').val($('#q').val().replace(/\x20+/g,' '));
20 is the Hex code for the space character. You were looking for all whitespace characters, including newlines.
How do I remove white spaces in a string but not new line character in JavaScript. I found a solution for C# , by using \t , but it's not supported in JavaScript.
To make it more clear, here's an example:
var s = "this\n is a\n te st"
using regexp method I expect it to return
"this\nisa\ntest"
[^\S\r\n]+
Not a non-whitespace char, not \r and not \n; one or more instances.
This will work, even on \t.
var newstr = s.replace(/ +?/g, '');
Although in Javascript / /g does match \t, I find it can hide the original intent as it reads as a match for the space character. The alternative would be to use a character collection explicitly listing the whitespace characters, excluding \n. i.e. /[ \t\r]+/g.
var newString = s.replace(/[ \t\r]+/g,"");
If you want to match every whitespace character that \s matches except for newlines, you could use this:
/[\t\v\f\r \u00a0\u2000-\u200b\u2028-\u2029\u3000]+/g
Note that this will remove carriage returns (\r), so if the input contains \r\n pairs, they will be converted to just \n. If you want to preserve carriage returns, just remove the \r from the regular expression.
Try this
var trimmedString = orgString.replace(/^\s+|\s+$/g, '') ;
This does the trick:
str.replace(/ /g, "")
and the space does NOT match tabs or linebreaks (CHROME45), no plus or questionmark is needed when replacing globally.
In Perl you have the "horizontal whitespace" shorthand \h to destinguish between linebreaks and spaces but unfortunately not in JavaScript.
The \t shorthand on the other hand IS supported in JavaScript, but it describes the tabulator only.
const str = "abc def ghi";
str.replace(/\s/g, "")
-> "abcdefghi"
try this '/^\\s*/'
code.replace(/^\s[^\S]*/gm, '')
works for me on text like:
#set($todayString = $util.time.nowEpochMilliSeconds())
#set($pk = $util.autoId())
$util.qr($ctx.stash.put("postId", $pk))
and removes the space/tabs before the first 3 lines with removing the spaces in the line.
*optimisation by #Toto:
code.replace(/^\s+/gm, '')