问题描述
采样输入(orgtext = A [CRLF】B【空间] C [CRLF])
我喜欢来存储每个字的a,b,c键字阵列与原始后缀CRLF或空间。目前,呼叫SPLIT下降后缀为分隔符,但我喜欢存储分离器为好。我可以调整正则表达式也返回后缀,还是分裂?
字=新的阵列;
变种AR:数组= orgtext.split(/ \ s + /);
对于(VAR我:= 0; I< ar.length;我++)
{
Words.push(AR [我] +后缀在这里);
}
通常你会用不断的打电话的的与使用全局(克一个前pression),使得将被设置。
VAR输入:字符串=ASD ASD ASD ASD;
VAR输出:阵列=新的Array();
VAR EXPR:正则表达式= / [^ \ s] +(?:$ | \ S +)/克;
VAR结果:对象= expr.exec(输入);
而(结果!= NULL)
{
input.push(导致[0]的ToString());
结果= expr.exec(输入);
}
根据比赛你可以预期的数量,这可能是更快地使用...
([^ \ s] +(?:$ | \ S +))+
...这将捕获所有可能的匹配在一个EXEC()。本场比赛将在结果可用[1] ...结果[N]
sample input (orgtext = a[crlf]b[space]c[crlf] )
I like to store each word a,b, c to the words array with the original suffix crlf or space. Currently calling SPLIT drops the suffix as its separator, but I like to store separator as well. Can I adjust regexp to return also suffix and still split?
Words = new Array;
var ar: Array = orgtext.split( /\s+/ );
for (var i:int = 0; i<ar.length;i++ )
{
Words.push( ar[i] +"suffix here" );
}
Generally you would use keep calling exec with an expression that uses the global (g) so that the lastIndex will be set.
var input : String = "asd asd asd asd";
var output : Array = new Array();
var expr : RegExp = /[^\s]+(?:$|\s+)/g;
var result : Object = expr.exec(input);
while(result != null)
{
input.push(result[0].toString());
result = expr.exec(input);
}
Depending on the number of matches you can expect, it might be faster to use...
([^\s]+(?:$|\s+))+
... which will capture all possible matches in one exec(). The matches will be available in result[1] ... result[n]
这篇关于使用正则表达式来分割字符串,但店内空白(空格或CRLF)项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!