如何使用JavaScript Regex提取字符串? [英] How to extract a string using JavaScript Regex?

查看:64
本文介绍了如何使用JavaScript Regex提取字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JavaScript Regex从文件中提取子字符串.这是文件的一部分:

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". Here is the approach:

extractSummary : function(iCalContent) {
  /*
  input : iCal file content
  return : Event summary
  */
  var arr = iCalContent.match(/^SUMMARY\:(.)*$/g);
  return(arr);
}

推荐答案

您需要使用m

多行;将开始和结束字符(^和$)视为有效 在多行上(即,匹配每行的开头或结尾 (以\ 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)

还将*放在正确的位置:

"DATE:20091201T220000\r\nSUMMARY:Dad's birthday".match(/^SUMMARY\:(.*)$/gm);
//------------------------------------------------------------------^    ^
//-----------------------------------------------------------------------|

这篇关于如何使用JavaScript Regex提取字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆