JavaScript Trim() And Normalize-space() Functions
Sep 29, 2005
Just wanted to share two handy RegEx expressions to strips leading and
trailing white-space from a string, and to replace all repeated spaces,
newlines and tabs with a single space.
* JavaScript example:
String.prototype.trim = function() {
// Strip leading and trailing white-space
return this.replace(/^s*|s*$/g, "");
}
String.prototype.normalize_space = function() {
// Replace repeated spaces, newlines and tabs with a single space
return this.replace(/^s*|s(?=s)|s*$/g, "");
}
" one two three
".trim(); // --> "one two three"
" one two three
".normalize_space(); // --> "one two three"
View 5 Replies
ADVERTISEMENT
Jun 20, 2006
If there is a unlimit long textfield, the user can type free text. It means that the client can type
I like eating very much.
How can I using regular expression to trim the spaces to single space?
View 4 Replies
View Related
Jul 23, 2005
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|
|
)*((.|
|
)*?)(s|
|
)*$/g,"$2")
//take consecutive spaces down to one
.replace(/(s(?!(
|
))(?=s))+/g,"")
//take consecutive lines breaks down to one
.replace(/(
|
)+/g,"
")
//remove spacing at the beginning of a line
.replace(/(
|
)s/g,"$1")
//remove spacing at the end of a line
.replace(/s(
|
)/g,"$1");
}
View 3 Replies
View Related
Apr 23, 2007
Using Regular Expressions (JavaScript 1.2/JScript 4+) :
String.prototype.lTrim =
function()
{
return this.replace(/^s+/,'');
}
String.prototype.lTrim =
function()
{
return this.replace(/s+$/,'');
}
String.prototype.trim =
function()
{
return this.replace(/^s+|s+$/g,'');
}
or for all versions (trims characters ASCII<32 not true
"whitespace"):
function LTrim(str) {
for (var k=0; k<str.length && str.charAt(k)<=" " ; k++) ;
return str.substring(k,str.length);
}
function RTrim(str) {
for (var j=str.length-1; j>=0 && str.charAt(j)<=" " ; j--) ;
return str.substring(0,j+1);
}
function Trim(str) {
return LTrim(RTrim(str));
}
http://msdn.microsoft.com/library/d...363906a7353.asp
http://docs.sun.com/source/816-6408-10/regexp.htm
View 27 Replies
View Related
Jun 6, 2007
I was trying:
Code:
<script>
function testtrim(value) {
alert(value.trim());
}
testtrim("Alex ");
</script>
The javascript don't work, my firebugs says:[value.trim is not a function] how to simulate an trim function?
View 4 Replies
View Related
Feb 25, 2009
I insert text at the text cursor in a contentEditable div. I then have 2 adjacent text nodes so I want to normalize them so there is only 1. The problem is that after I normalize I lose focus on the div and my [text] cursor position. Is there a way around this? How can I save my cursor position and go back to it after I normalize?
View 5 Replies
View Related
Oct 9, 2006
I wrote a JavaScript that removes the white space from javascript source code in order to speed up download times.
For instance:
View 3 Replies
View Related
Jul 23, 2005
I am learning HTML for the first time taking a self teaching class
though my local Community College. Normally this college rocks and has
some of the best resources and down to earth teachers that pick books
that acutally help folks.
Well they failed and my book take more logic jumps that Stephen
Hawkins! :D
So my ultimate question is as follows:
How do I created a function with the following information provided:
Create a fucntion named Mquote that contains the single parameter,
Qnum.
My apologies for such little information. I am sure its my oversight
that I am unable to locate the answer to my question.
What I am looking for is the base layout for noob java functions.
View 7 Replies
View Related
Apr 18, 2006
Can javascript functions have variable number of arguments - a concept
known as paramarrays?
View 15 Replies
View Related
Sep 21, 2007
I have some javascript functions in an xslt file. Everything works and produces the desired html, except when using the visual studio debugger which gives the error "objects of type 'Script1' do not have such a member" and references the line and column where the "this" object is used, as in this.hourarray()
View 1 Replies
View Related
Dec 18, 2001
I wrote a funtion for all my <a href=...> tags. If I use the function, it looks like this:
<a href="test.html" onFocus="myfunction()">
What I want is to automatically add myfunction() to all the links on a page. So I don't want to add onFocus=blablabla each time when I link to another page.
View 5 Replies
View Related
Jan 30, 2007
I have this code:
Code:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<script language="javascript" >
function outer(data)
{
var operand1 = data;
function inner(operand2)
{
alert(operand1 + operand2)
}
}
</script>
<body>
//<input type="button" value="1" onclick="outer(3)(2)" />
//<input type="button" value="2" onclick="outer(3)(2)" />
<div id="target"></div>
</body>
</html>
How do I call the inner function and pass operand?
View 2 Replies
View Related
Jul 23, 2005
We have an applet that has to support the SUN VMs as well as the MS VM.
The applet receives updates from a server (via tcp or http) and wraps them
up as objects and passes them using the JSObject scripting context to a
javascript function.
This function takes the object and reads the properties and updates a
screen.
All seems simple stuff. The problem is that the MS JVM runs like a rocket,
but the sun vm seems to max out our processor and also takes ages to process
anything.
I have tried logging from the console using debug level 5, and it shows a
lot of back and forth between the applet and the javascript when accessing
methods on the object passed.
Seeing that as a possible problem, I now pass a delimited string to the
front end, and then convert that into an update message purely in javascript
so there is only one hit to the applet per message.
I am still seeing a massive difference between the sun and ms VMs.
Has anyone ever come across this, and is there anything I can do to help
flatten out this performance?
View 1 Replies
View Related
Sep 23, 2005
Here is a compilation/creation/collection of recreated vbscript functions for use in JavaScript. Additions and criticism are of course welcome.
Zip file contains:
vbs.js
ReadMe.html
<Edit>Surprisingly there haven't been many downloads for this. Here's a quick overview of the contents:</Edit>
==================================================
==================================================
CreateObject(x)
Translates into: new ActiveXObject(x)
dateDiff(p_Interval, p_Date1, p_Date2,p_firstdayofweek, p_firstweekofyear)
Returns the number of intervals between two dates
datePart(p_Interval, p_Date,p_firstdayofweek, p_firstweekofyear)
Returns the specified part of a given date.
FormatCurrency(Expression, NumDigitsAfterDecimal,IncludeLeadingDigit,UseParensForNegativeNumbers, GroupDigits)
FormatDateTime(datetime, FormatType)
FormatNumber(Expression, NumDigitsAfterDecimal,IncludeLeadingDigit,UseParensForNegativeNumbers, GroupDigits)
FormatPercent(Expression, NumDigitsAfterDecimal,IncludeLeadingDigit,UseParensForNegativeNumbers, GroupDigits)
InStr(strSearch,charSearchFor)
Returns the first location a substring SearchForStr that was found in the string str
isDate(p_Expression)
Left(string, length)
Returns a specified number of characters from the left side of a string.
Len(str)
Returns the number of characters in a string.
LTrim(str)
Returns a copy of a string without leading spaces.
Mid(str,start,len)
Returns a specified number of characters from a string.
monthName(p_Date, p_abbreviate)
Now()
Returns the current date and time.
Right(string, length)
Returns a specified number of characters from the right side of a string
RTrim(string)
Returns a copy of a string without trailing spaces.
Trim(strInput)
Removes leading and trailing spaces
weekdayName(p_Date, p_abbreviate)
================================================
================================================
View 1 Replies
View Related
Apr 1, 2006
Might there be a way to add a set of Javascript functions to a display page's existing set of Javascript functions on the fly without reloading the display page, and if so, how?
A couple of approaches I have tried, but without success are:
- A) For an embedded "retriever" IFrame in the display page to retrieve a set of JS functions from the server and then on its "onload" transfer (by innerHTML transference) that set of functions into the parent display page.
- B) Similiar to "A" but to use regular old browser frames - one being the "retriever" and one being the display page.
- C) Using a JS function in the display page to make a http request for a supplementary .js file(s).
Please note: the goal is to retrieve *only* an additional set of Javascript functions from the server, and then add them to an "original" set of functions already loaded into the browser. Meaning, NOT - say - for an IFrame to download from the server *both* the additional set and again the "original" set, which the parent display page could then run directly from the IFrame.
After "A", "B", and "C", I'm at a loss for further ideas.
View 2 Replies
View Related
Dec 5, 2007
I have a form with 2 submit buttons, "Test" and "Save".
I also have to javascript functions, "TestImage" and "SaveImage".
What I want to do is, when the "Test" button get's clicked, then execute the "TestImage" function. That was easy, I did it like this:
<input type="submit" name="test" value="Test" onClick="return TestImage(this);">
But what I also want is, when someone clicks on the "Save" button, to first execute the "TestImage" function, and then the "SaveImage" function. Anyone know how I can do this?
View 7 Replies
View Related
Mar 22, 2007
i am having trouble understanding what the difference is
between BLUR and FOCUS. I need to include these functions in my form
and i am unsure what these features do and how i incorporate these
into my form.
View 6 Replies
View Related
Mar 20, 2006
Are there javascript functions to find the cursor position of a mouse? Also, are there javascript functions to find if the mouse cursor is within a listbox? A C# code sample is shown below:
Listbox lb; //lb is a listbox
Point cpos = lb.PointToClient(Cursor.Position); //find coordinates of mouse
if(lb.ClientRectangle.Contains(cpos)) //if mouse cursor is within the listbox
Are there any javascript functions which can do the same as the above c# methods?
View 1 Replies
View Related
Nov 4, 2005
I wrote some prototype functions in my code for arrays this is one of them !
Array.prototype.inArray = function(value){
for (var x in this) {
if (this[x] === value) {
return true;
}
}
return false;
};
when I iterate through array .. I get this functions as values in array !!!
can anybody explain this !?!?
View 11 Replies
View Related
Jan 8, 2007
I want to learn more about creating functions for element events
without having to put onclick event in every tag. For example:
<script>
document.getElementsByTagName('input').onclick = function () {
alert('hello');
}
</script>
Now, I know the above does not work as I have tried it but hopefully
the idea of what I am trying to achieve here. Basically for every input
tag, when the user triggers the event (click) it will do the same
function.
Anyway, I would more so like to learn about this type of scripting
where you assign functions to events. However, I dont know what to
search for in google and the like. Where could I learn more about this?
View 2 Replies
View Related
Nov 25, 2009
First the code:
Code:
Looking at the code, you might assume (as I did) that clicking the button on page1.htm would load page2.htm. However for some reason, when calling a parent function, the function runs with the calling file's path rather than the path of the parent.
So it is actually trying to load /files/files/page2.htm! How do I force all calls to parent.function() to run with parent.hta's path as the root?
Realize that this is a simplistic example and simple solutions such as putting all the files in the same path, or dropping the "/files" in the loadPage2() function are not valid solutions.
Essentially I need to call parent.loadPage2() and have it run the function with the root path = parent.hta's path.
View 3 Replies
View Related
Nov 29, 2005
Does java script provides inbuilt trimming function, if not then what
is the solution.
View 2 Replies
View Related
Feb 26, 2006
How do we trim out or remove a part of a URL or path?
eg. http://www.mywebsite/folder/somefile.htm ----I want to trim
http://www.mywebsite/folder/ so All I have left is the file name.
<script language="JavaScript">
var pathtotrim = location.href;
document.write (pathtotrim);
</script>
View 10 Replies
View Related
May 31, 2009
Is there really no string trim function in JS?
Code:
function trim(s)
{
return rtrim(ltrim(s));
[code].....
View 3 Replies
View Related
Oct 2, 2009
I'm working with SageCRM. When SageCRM outputs the company address, I kid you not, it outputs the value and then a crap ton of HTML non-breaking spaces, a break tag and then repeat for the other address lines.My client added a button to the page via the customization function that links over to MapQuest. But, all those non-breaking spaces mess up the URL.I'm trying to fix it, but I'm having some trouble and thought I'd throw it out to you all.
Code:
// Ninja'd this from somewhere to trim whitespace.
function trim(stringToTrim) {
[code]....
View 1 Replies
View Related
May 8, 2009
I have the following script that converts line breaks from plain text into HTML formatted paragraphs. It takes plain text from one text area field and outputs the new formatted text into another text area field.
function convertText(){
var noBreaks = document.getElementById("oldText").value;
noBreaks = noBreaks.replace(/
[code]....
View 8 Replies
View Related