Folks,
In PHP and some other scripting languages, one has trim() - It removes
newline, tabs and blank spaces that might prefix, or suffix a string.
Can someone tell me how I can do this in javascript?
Much appreciated,
randell d. 11 4705
Reply Via Newsgroup wrote: In PHP and some other scripting languages, one has trim() - It removes newline, tabs and blank spaces that might prefix, or suffix a string.
Can someone tell me how I can do this in javascript? http://jibbering.com/faq/#FAQ4_16
JW
Reply Via Newsgroup wrote: Folks,
In PHP and some other scripting languages, one has trim() - It removes newline, tabs and blank spaces that might prefix, or suffix a string.
Can someone tell me how I can do this in javascript?
Much appreciated, randell d. http://www.vermontsoftware.com/Javascript/trim.html
pcx99 wrote: Reply Via Newsgroup wrote: Folks,
In PHP and some other scripting languages, one has trim() - It removes newline, tabs and blank spaces that might prefix, or suffix a string.
Can someone tell me how I can do this in javascript?
Much appreciated, randell d.
http://www.vermontsoftware.com/Javascript/trim.html
String.prototype.trim = function()
{
return this.match(/(\b\S+|\S+)/g).join(" ");
}
On Sun, 02 May 2004 07:03:20 -0500, Fox <fo*@fxmahoney.com> wrote:
[snip] String.prototype.trim = function() { return this.match(/(\b\S+|\S+)/g).join(" "); }
The FAQ version seems to be more logical.
Mike
--
Michael Winter M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
JRS: In article <op**************@news-text.blueyonder.co.uk>, seen in
news:comp.lang.javascript, Michael Winter <M.******@blueyonder.co.invali
d> posted at Sun, 2 May 2004 15:52:03 : On Sun, 02 May 2004 07:03:20 -0500, Fox <fo*@fxmahoney.com> wrote:
[snip]
String.prototype.trim = function() { return this.match(/(\b\S+|\S+)/g).join(" "); }
The FAQ version seems to be more logical.
More importantly, it does what "randell d." asked for, whereas the
quoted code also converts multiple spaces within the string to singles.
That task can, AFAICS, be done by S.split(/\s+/).join(" "); and that
task might be worth including in the FAQ section, with optimum solution.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Dr John Stockton <sp**@merlyn.demon.co.uk> wrote in message news:<to**************@merlyn.demon.co.uk>... JRS: In article <op**************@news-text.blueyonder.co.uk>, seen in news:comp.lang.javascript, Michael Winter <M.******@blueyonder.co.invali d> posted at Sun, 2 May 2004 15:52:03 :On Sun, 02 May 2004 07:03:20 -0500, Fox <fo*@fxmahoney.com> wrote:
[snip]
String.prototype.trim = function() { return this.match(/(\b\S+|\S+)/g).join(" "); }
The FAQ version seems to be more logical.
More importantly, it does what "randell d." asked for, whereas the quoted code also converts multiple spaces within the string to singles.
That task can, AFAICS, be done by S.split(/\s+/).join(" "); and that task might be worth including in the FAQ section, with optimum solution.
The FAQ version is clearly the correct way to trim strings. However,
I'm curious as to the reason the FAQ prototype is generated with "new
Function(str)" instead of the more common function-expression form
given above.
Strings introduce the requirement for double escaping the "\" (as per
new RegExp), and while such use may be instructive, it adds
chicken-track complexity where in a place where it doesn't appear to
be necessary.
Am I missing some subletly regarding browser compatibility, or
something to do with "this" association?
../rh
Reply Via Newsgroup <re****************@please.com> wrote in message news:<FMjcc.30509$Pk3.9550@pd7tw1no>... Folks,
In PHP and some other scripting languages, one has trim() - It removes newline, tabs and blank spaces that might prefix, or suffix a string.
Can someone tell me how I can do this in javascript?
Much appreciated, randell d.
There is a quick, easy way to do this with a regular expression.
It only takes one line of Javascript.
Code written, commented, and tested by me. No warranty. :o)
Shawn
<script type="text/javascript">
var testString = ' there are leading and trailing spaces (and tabs) ';
alert('\'' + testString + '\'');
//regex explanation:
//regex is in forward slashes: //
// syntax string = string.replace(/pattern/, 'replace with this');
^ = beginning of line
(\s+)? = one or more characters of whitespace, optional
(.*\S) = any characters, with the last one not being whitespace
(\s+)? = one or more characters of whitespace, optional
$ = end of line
$2 = what was in the 2nd set of parenthesis
testString = testString.replace(/^(\s+)?(.*\S)(\s+)?$/, '$2');
alert('\'' + testString + '\'');
</script>
rh wrote: Fox <fo*@fxmahoney.com> wrote: [...] String.prototype.trim = function() { return this.match(/(\b\S+|\S+)/g).join(" "); } The FAQ version is clearly the correct way to trim strings. However, I'm curious as to the reason the FAQ prototype is generated with "new Function(str)" instead of the more common function-expression form given above.
Strings introduce the requirement for double escaping the "\" (as per new RegExp), and while such use may be instructive, it adds chicken-track complexity where in a place where it doesn't appear to be necessary.
Depends.
Am I missing some subletly regarding browser compatibility,
Yes, indeed you do. If Netscape's Core JavaScript Reference 1.5 and the
MSDN Library is to be trusted, the Function() constructor is available from
JavaScript 1.1 (NN3+) and JScript 2.0 (IE/IIS 3+) on, while the "function"
statement within another statement (including itself) or the "function"
operator (also defining anonymous functions) requires JavaScript 1.2 (NN4+)
or 1.5 (NN6+; in JScript AFAIS both are available in all versions and thus
all UAs), respectively. (Unfortunately, the Reference cannot be trusted
here since I had to learn that NN 4.7 which claims to support JavaScript
up to 1.3 only also supports the "function" operator.)
As for the ECMAScript standard, the "function" statement within a
statement is AFAIS not available before edition 3 as there is no
FunctionExpression to be produceable through the MemberExpression production.
While the UAs given respect to herewith may seem outdated, there may
be still UAs around which implement only those older versions of the
respective language(s). However, we also had one case of a buggy
implementation here that had b0rken Function() constructor support.
Google is your friend. [psf 6.1]
<http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/function.html#1193137>
<http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/stmt.html#1004825>
<http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/ops.html#1066344>
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsoriVersionInformation.asp>
or something to do with "this" association?
No.
String.prototype.trim = new Function(
"return this.match(/(\\b\\S+|\\S+)/g).join(' ');");
is semantically equal to the above.
HTH
PointedEars
for all your javascript trimming needs...
function trim(inputString) {
if (typeof inputString != "string") return inputString;
return inputString
//clear leading spaces and empty lines
.replace(/^(\s|\n|\r)*((.|\n|\r)*?)(\s|\n|\r)*$/g,"$2")
//take consecutive spaces down to one
.replace(/(\s(?!(\n|\r))(?=\s))+/g,"")
//take consecutive lines breaks down to one
.replace(/(\n|\r)+/g,"\n\r")
//remove spacing at the beginning of a line
.replace(/(\n|\r)\s/g,"$1")
//remove spacing at the end of a line
.replace(/\s(\n|\r)/g,"$1");
}
-- Jon
pcx99 <x@x.com> wrote in message news:<eg****************@newssvr23.news.prodigy.co m>... Reply Via Newsgroup wrote: Folks,
In PHP and some other scripting languages, one has trim() - It removes newline, tabs and blank spaces that might prefix, or suffix a string.
Can someone tell me how I can do this in javascript?
Much appreciated, randell d.
http://www.vermontsoftware.com/Javascript/trim.html
function TrimSpacesFromBeginAndEnd (str)
{
pattern1=/[\s]+$/
pattern2=/^[\s]+/
y=str.replace(pattern2,"").replace(pattern1,"")
return y
}
This is assuming no unprintable characters...
Thomas 'PointedEars' Lahn <Po*********@nurfuerspam.de> wrote in message news:<40**************@PointedEars.de>... rh wrote:
Fox <fo*@fxmahoney.com> wrote: > [...] > String.prototype.trim = function() > { > return this.match(/(\b\S+|\S+)/g).join(" "); > } The FAQ version is clearly the correct way to trim strings. However, I'm curious as to the reason the FAQ prototype is generated with "new Function(str)" instead of the more common function-expression form given above.
Strings introduce the requirement for double escaping the "\" (as per new RegExp), and while such use may be instructive, it adds chicken-track complexity where in a place where it doesn't appear to be necessary.
Depends.
Am I missing some subletly regarding browser compatibility,
Yes, indeed you do. If Netscape's Core JavaScript Reference 1.5 and the MSDN Library is to be trusted, the Function() constructor is available from JavaScript 1.1 (NN3+) and JScript 2.0 (IE/IIS 3+) on, while the "function" statement within another statement (including itself) or the "function" operator (also defining anonymous functions) requires JavaScript 1.2 (NN4+) or 1.5 (NN6+; in JScript AFAIS both are available in all versions and thus all UAs), respectively. (Unfortunately, the Reference cannot be trusted here since I had to learn that NN 4.7 which claims to support JavaScript up to 1.3 only also supports the "function" operator.)
As for the ECMAScript standard, the "function" statement within a statement is AFAIS not available before edition 3 as there is no FunctionExpression to be produceable through the MemberExpression production.
While the UAs given respect to herewith may seem outdated, there may be still UAs around which implement only those older versions of the respective language(s). However, we also had one case of a buggy implementation here that had b0rken Function() constructor support. Google is your friend. [psf 6.1]
<http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/function.html#1193137> <http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/stmt.html#1004825> <http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/ops.html#1066344> <http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsoriVersionInformation.asp>
or something to do with "this" association?
No.
String.prototype.trim = new Function( "return this.match(/(\\b\\S+|\\S+)/g).join(' ');");
is semantically equal to the above.
HTH
It does, thanks for taking the time to lay it out.
(I'm glad to see you got by my misspelling of subtlety above. Now if
we can just get you to use "broken" instead of "borken" and "b0rken"
-- or is this another psf-ism? :))
../rh PointedEars This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Rohit Yogi |
last post by:
I have few scripts which use rtrim, ltrim & trim.
I want to know what is use of these functions what do the do.
1 example of SQL is:
update ARRANGEMENT set DOCUMENT=trim(DOCUMENT);
commit;
|
by: Simon |
last post by:
Hi,
I have written a function to trim char *, but I have been told that my way
could be dangerous and that I should use memmove(...) instead.
but I am not sure why my code could be 'dangerous'...
|
by: Darren Anderson |
last post by:
I have a function that I've tried using in an if then statement and I've
found that no matter how much reworking I do with the code, the expected
result is incorrect.
the code:
If Not...
|
by: Sascha Herpers |
last post by:
Hi,
what is the difference between the trim function and the trim
String-member?
As far as I see it, both return the trimmed string and leave the
original string unaltered.
Is any of the two...
|
by: Terry Olsen |
last post by:
I have an app that makes decisions based on string content. I need to make
sure that a string does not contain only spaces or newlines. I am using the
syntax 'Trim(String)" and it works fine. I...
|
by: Pascal |
last post by:
bonjour hello
I would like to trim a string of all its white spaces so i used
myString.trim() but it doesn't work as supposed : unsecable space are
remaining in the middle of my string...
i...
|
by: rkk |
last post by:
Hi,
I've written a small trim function to trim away the whitespaces in a
given string. It works well with solaris forte cc compiler, but on
mingw/cygwin gcc it isn't. Here is the code:
char...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |