文章出處
String.js
文章列表
有些時候,我們確實需要在JavaScript中進行字符串替換,類似于C#中的String.Format()方法一樣,只不過這種格式化替換只局限于對由'{0}','{1}','{2}'...所組成的“占位符”進行字符串替換,而并不會像C#中可以進行字符串格式化替換。這會大量簡化我們的代碼,使得程序結構變得更加清晰。眾所周知,JavaScript中的replace方法默認只能對第一個匹配到的字符串進行替換,如果給定的字符串中存在多個匹配項,則除了第一個匹配項之外其余的部分都不會被替換。因此我們可以借助于正則表達式來進行替換。
來看看下面這個方法:
if (!String.prototype.format) { String.prototype.format = function () { var args = arguments; return this.replace(/{(\d+)}/g, function (match, number) { return typeof args[number] != 'undefined' ? args[number] : match ; }); }; }
如何使用?
"{0} is dead, but {1} is alive! {0} {2}".format("ASP", "ASP.NET");
ASP is dead, but ASP.NET is alive! ASP {2}
注意'{2}'沒有被替換,因為參數列表中沒有給定對應的值。在該方法中,沒有對參數列表和占位符進行嚴格限制。也就是說,允許占位符中的數字不連續或者一定要從0開始,而且參數列表的數量并不一定要與占位符中的數字完全對應。只對正則表達式匹配到的項進行查找替換。所以該方法可以滿足一些基本應用。
這里還有另外一個JavaScript類庫。

// String.js - liberated from MicrosoftAjax.js on 03/28/10 by Sky Sanders /* Copyright (c) 2009, CodePlex Foundation All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of CodePlex Foundation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</textarea> */ (function(window) { $type = String; $type.__typeName = 'String'; $type.__class = true; $prototype = $type.prototype; $prototype.endsWith = function String$endsWith(suffix) { /// <summary>Determines whether the end of this instance matches the specified string.</summary> /// <param name="suffix" type="String">A string to compare to.</param> /// <returns type="Boolean">true if suffix matches the end of this instance; otherwise, false.</returns> return (this.substr(this.length - suffix.length) === suffix); } $prototype.startsWith = function String$startsWith(prefix) { /// <summary >Determines whether the beginning of this instance matches the specified string.</summary> /// <param name="prefix" type="String">The String to compare.</param> /// <returns type="Boolean">true if prefix matches the beginning of this string; otherwise, false.</returns> return (this.substr(0, prefix.length) === prefix); } $prototype.trim = function String$trim() { /// <summary >Removes all leading and trailing white-space characters from the current String object.</summary> /// <returns type="String">The string that remains after all white-space characters are removed from the start and end of the current String object.</returns> return this.replace(/^\s+|\s+$/g, ''); } $prototype.trimEnd = function String$trimEnd() { /// <summary >Removes all trailing white spaces from the current String object.</summary> /// <returns type="String">The string that remains after all white-space characters are removed from the end of the current String object.</returns> return this.replace(/\s+$/, ''); } $prototype.trimStart = function String$trimStart() { /// <summary >Removes all leading white spaces from the current String object.</summary> /// <returns type="String">The string that remains after all white-space characters are removed from the start of the current String object.</returns> return this.replace(/^\s+/, ''); } $type.format = function String$format(format, args) { /// <summary>Replaces the format items in a specified String with the text equivalents of the values of corresponding object instances. The invariant culture will be used to format dates and numbers.</summary> /// <param name="format" type="String">A format string.</param> /// <param name="args" parameterArray="true" mayBeNull="true">The objects to format.</param> /// <returns type="String">A copy of format in which the format items have been replaced by the string equivalent of the corresponding instances of object arguments.</returns> return String._toFormattedString(false, arguments); } $type._toFormattedString = function String$_toFormattedString(useLocale, args) { var result = ''; var format = args[0]; for (var i = 0; ; ) { // Find the next opening or closing brace var open = format.indexOf('{', i); var close = format.indexOf('}', i); if ((open < 0) && (close < 0)) { // Not found: copy the end of the string and break result += format.slice(i); break; } if ((close > 0) && ((close < open) || (open < 0))) { if (format.charAt(close + 1) !== '}') { throw new Error('format stringFormatBraceMismatch'); } result += format.slice(i, close + 1); i = close + 2; continue; } // Copy the string before the brace result += format.slice(i, open); i = open + 1; // Check for double braces (which display as one and are not arguments) if (format.charAt(i) === '{') { result += '{'; i++; continue; } if (close < 0) throw new Error('format stringFormatBraceMismatch'); // Find the closing brace // Get the string between the braces, and split it around the ':' (if any) var brace = format.substring(i, close); var colonIndex = brace.indexOf(':'); var argNumber = parseInt((colonIndex < 0) ? brace : brace.substring(0, colonIndex), 10) + 1; if (isNaN(argNumber)) throw new Error('format stringFormatInvalid'); var argFormat = (colonIndex < 0) ? '' : brace.substring(colonIndex + 1); var arg = args[argNumber]; if (typeof (arg) === "undefined" || arg === null) { arg = ''; } // If it has a toFormattedString method, call it. Otherwise, call toString() if (arg.toFormattedString) { result += arg.toFormattedString(argFormat); } else if (useLocale && arg.localeFormat) { result += arg.localeFormat(argFormat); } else if (arg.format) { result += arg.format(argFormat); } else result += arg.toString(); i = close + 1; } return result; } })(window);
<script src="script/string.js" type="text/javascript"></script> <script type="text/javascript"> var a = String.format("Hello {0}!", "world"); alert(a); </script>
另外,我的另一篇博客中有關于如何在JavaScript中對字符串進行Trim操作的例子:
文章列表
全站熱搜