|
| Trim i Javascript ? Fra : lars |
Dato : 26-11-05 13:38 |
|
I VBScipt brugte jeg tit Trim til at fjerne fx. mellemrum fra
ordet, men hvordan skal jeg kunne gør det samme i Javacript ?
mvh lars k.
--
Vil du lære at kode HTML, XHTML, CSS, SSI, ASP eller ASP.NET?
- Pædagogiske tutorials på dansk
- Kom godt i gang med koderne
KLIK HER! => http://www.html.dk/tutorials
| |
Jørn Andersen (26-11-2005)
| Kommentar Fra : Jørn Andersen |
Dato : 26-11-05 14:50 |
|
On 26 Nov 2005 12:37:55 GMT, lars <hp@s.dk> wrote:
>I VBScipt brugte jeg tit Trim til at fjerne fx. mellemrum fra
>ordet, men hvordan skal jeg kunne gør det samme i Javacript ?
Der er et eksempel i Windows Script dokumentationen:
---------------------
Using this principle, you can define additional properties for
predefined constructor functions (which all have prototype objects).
For example, if you want to be able to remove leading and trailing
spaces from strings (similar to VBScript's Trim function), you can
create your own method on the String prototype object, and all strings
in your script will automatically inherit the method.
// Add a function called trim as a method of the prototype
// object of the String constructor.
String.prototype.trim = function()
{
// Use a regular expression to replace leading and trailing
// spaces with the empty string
return this.replace(/(^\s*)|(\s*$)/g, "");
}
// A string with spaces in it
var s = " leading and trailing spaces ";
// Displays " leading and trailing spaces (35)"
window.alert(s + " (" + s.length + ")");
// Remove the leading and trailing spaces
s = s.trim();
// Displays "leading and trailing spaces (27)"
window.alert(s + " (" + s.length + ")");
---------------------
- skær selv det unødvendige fra.
Good luck,
Jørn
--
Jørn Andersen,
Brønshøj
| |
|
|