- 浏览: 401184 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
string.replace method accept a regexpression as the seperator, also, you can pass some anonymous function which will work on each of the matched method;
let's see the example.
/************************************** *@Summary * this file demonstrate the use of string.replace method, which can accept some regexpression and an additional function which works on each and every element that is returned by the match * * * @Usage: * * compress( "foo=1&foo=2&blah=a&blah=b&foo=3" ) == "foo=1,2,3&blah=a,b" * * @TODO: * some more practical use of the string.replac method call * assert("a b c".replace(/a/, function() { return ""; } ) == " b c", "returning an empty result removes a match"); ***************************************/ function compress(data) { var q = [], ret = []; // in the anonymouse function, the first argument is the entire match, and followed by each, subsequent capture following. data.replace(/([^=&]+)=([^&]*)/g, function(m, key, value) { q[key] = (q[key] ? q[key] + "," : "") + value; // why do we need to return a empty string? // the return value of the replace method call will be injected back to the string as replacement. // by returning a empty string, we are clearning the matched values. return ""; }); for (var key in q) { ret.push(key + "=" + q[key]); } return ret.join("&"); }
and below is the code that test the functions above, which also shows you the tricks on how to make use of the return value from the anonymous function.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script type="text/javascript" src="strregex.js" ></script> <script type="text/javascript" src="../unit.js"></script> <script type="text/javascript"> window.onload = function () { test("test string replace method call", function () { assert(compress("foo=1&foo=2&blah=a&blah=b&foo=3") == "foo=1,2,3&blah=a,b", "verity the compression method"); }); test("Test string replace method 2", function () { assert("a b c".replace(/a/, function () { return ""; }) == " b c", "Returning an empty result removes a match."); }); }; </script> <style> #results li.pass { color: Green } #results li.fail { color: Red} </style> </head> <body> <ul id="results" /> </body> </html>
发表评论
-
javascript - trick to cross browser DOM ready event
2012-08-24 08:23 930the "ready" event ... -
javascript - trick to simulate mouseenter and mouseleave
2012-08-23 08:31 2256Previously we discussed javasc ... -
javascript - trick to simulate the change event
2012-08-22 08:51 1671In the previous discussion a ... -
javascript - trick to simulate bubbling submit event
2012-08-22 08:03 909In the previous discussion abou ... -
javascript - trick to implement bubbling submit event
2012-08-23 07:55 707Following up to the javascrip ... -
javascript - trick to detect bubbling supportability
2012-08-20 22:22 975Event delegation is oe of the b ... -
javascript - trigger event and custom events
2012-08-20 21:58 2084In the previous post - javascri ... -
javascript - trick to handlers management
2012-08-20 08:19 1033We have discussed "javascr ... -
javascript - trick to centralized store
2012-08-20 07:52 823For a number of reasons it's ... -
javascript - trick to fix the event object
2012-08-20 07:47 886Many browsers, especially In ... -
javascript - tricks to deal with colors
2012-08-15 08:34 770There are a couple of ways to r ... -
javascript - trick to manipulate the opacity
2012-08-15 08:26 770All other browsre may have supp ... -
javascript - trick to test visibility of an element
2012-08-15 08:15 525though there is a visible prope ... -
javascript - trick to get and set height and width
2012-08-15 08:05 550when looking at properties t ... -
javascript - trick to set/get attributes that expects px values
2012-08-16 11:00 521When setting a number into a ... -
javascript - trick to get and set CSS style
2012-08-16 11:00 749while it will not be so much tr ... -
javascript - trick to normalize href for IE
2012-08-16 10:59 538IE is again the only browser th ... -
javascript - trick IE form and its expando attribute
2012-08-16 10:59 1044there is a known issue that if ... -
javascript expando and attributes
2012-08-14 08:15 1045expando is something like this ... -
javascript - trick to getText and setText
2012-08-14 07:40 1150it is not as simple as you thin ...
相关推荐
javascript-String.rar
字符替换是一个非常重要的功能,javascript 中有一个 String.replace( ),但是此方法有很多不为新手所知的妙用,如果用的好了,可以为您节省很多宝贵时间,还等什么?马上进来看看吧!
JavaScript 中的 location.reload 和 location.replace 方法的区别 在 JavaScript 中,我们经常使用 location.reload() 和 location.replace() 两个方法来刷新页面或重定向到新的 URL。但是,这两个方法之间有着...
这个"matlab开发-未经许可的Javascriptstring.zip.zip"文件可能包含了关于如何在MATLAB中使用JavaScript字符串处理技术的相关资料,尽管它提及“未经许可”,这可能意味着其中的内容可能是版权保护或者非官方的教程...
JS字符串函数String.replace()是JavaScript中一个非常实用的字符串操作方法,它允许开发者针对字符串中的文本进行替换。这个函数对于文本处理非常重要,因为它可以应用正则表达式来实现复杂的模式匹配和替换逻辑。 ...
本文实例讲述了JavaScript中String.prototype用法。分享给大家供大家参考。具体如下: // 返回字符的长度,一个中文算2个 String.prototype.ChineseLength=function() { return this.replace(/[^\x00-\xff]/g,"**...
原生的JavaScript自ECMAScript5标准引入后,就已经提供了`String.trim()`方法来去除字符串首尾的空白字符,这为开发者带来了极大的便利。然而,在此之前,或者在某些特定环境下(如旧版本的浏览器),`String.trim()...
在JavaScript中,`String.replace()`函数是一个非常实用的工具,用于在字符串中寻找特定的字符或模式,并将其替换为其他字符或字符串。这个方法对于文本处理、格式化以及数据清洗等场景尤其有用。 `String.replace...
java 的 String.format() 的 Javascript 实现,增加了支持格式化百分比和后缀-SI,如 120M、30K。 将 Formatter.format() 移植到 javascript 的基本开始。 目前依赖 .toLocaleString() 进行命名日期格式。 请参阅...
字符串替换异步知道如何等待的“ string” .replace()函数安装$ npm install string-replace-async用法let replaceAsync = require ( "string-replace-async" ) ;await replaceAsync ( "#rebeccapurple" , / # ( \...
在JavaScript中,没有内置的`String.Format`函数,如C#中那样,它提供了一种方便的方式来格式化字符串。然而,由于JavaScript的灵活性,我们可以创建一个类似的函数来实现这一功能。`String.Format`的主要作用是将...
The 6th edition covers HTML5 and ECMAScript 5, with new chapters on jQuery and server side JavaScript. It's recommended for experienced programmers who want to learn the programming language of the ...
string.prototype.replaceall 用于String.prototype.replaceAll的ES Proposal规范填充程序。 如果不可用或不String.prototype....// replaceAll and replace are the same, when given a global regex to replace as
<title>JavaScript String.match() Method <script type="text/javascript"> var str = "For more information, see Chapter *.*.*.*"; var re = /(chapter\d+(\.\d)*)/i; var found = str.match(re); document....
然而,JavaScript原生并不提供像C#或Java那样的`String.Format`方法,这使得在需要格式化字符串时,开发者通常需要使用加号(+)或者模板字符串(ES6引入的新特性)来组合字符串和变量。这在处理复杂格式化需求时...
- **标题**: "[JavaScript权威指南(第6版)].(JavaScript:The Definitive Guide).David Flanagan.文字版.pdf" - **描述**: "[JavaScript权威指南(第6版)].(JavaScript:The Definitive Guide).David Flanagan.文字版...