如何使用Javascript Regex提取字符串 [英] How to extract a string using Javascript Regex
问题描述
它可能看起来很明显,但我浪费了太多时间试图让它工作......
it might look obvious but I wasted too much time trying to get it to work...
我正在尝试使用Javascript从文件中提取子字符串正则表达式。这是文件中的一个切片:
I'm trying to extract a substring from a file with Javascript Regex. Here is a slice from the file :
DATE:20091201T220000
SUMMARY:Dad's birthday
我要提取的字段是摘要,所以我正在尝试编写一个只返回摘要文本的方法。以下是方法:
the field I want to extract is Summary, so I'm trying to write a method that returns only the summary text. Here is the method :
extractSummary : function(iCalContent) {
/*
input : iCal file content
return : Event summary
*/
var arr = iCalContent.match(/^SUMMARY\:(.)*$/g);
return(arr);
}
显然,我是一个正则表达式noob :))你能解决它吗? ?谢谢
clearly, I'm a Regex noob :)) could you fix it please ? thanks
推荐答案
你需要使用 m
flag :
multiline;将开头和结尾字符(^和$)视为在多行上工作
(即匹配每行的开头或结尾
(由\ n或\r分隔),不仅非常
整个输入字符串的开头或结尾)
multiline; treat beginning and end characters (^ and $) as working over multiple lines (i.e., match the beginning or end of each line (delimited by \n or \r), not only the very beginning or end of the whole input string)
同时输入 *
在正确的位置:
Also put the *
in the right place:
"DATE:20091201T220000\r\nSUMMARY:Dad's birthday".match(/^SUMMARY\:(.*)$/gm);
//------------------------------------------------------------------^ ^
//-----------------------------------------------------------------------|
这篇关于如何使用Javascript Regex提取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!