编程语言
首页 > 编程语言> > 如何使用JavaScript Regex提取字符串?

如何使用JavaScript Regex提取字符串?

作者:互联网

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

DATE:20091201T220000
SUMMARY:Dad's birthday

我要提取的字段是“摘要”.这是方法:

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

解决方法:

您需要使用m flag

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,string,regex,icalendar
来源: https://codeday.me/bug/20190926/1822137.html