English 中文(简体)
如何利用贾瓦·雷特克斯进行扼杀?
原标题:How to extract a string using JavaScript Regex?

我试图从Javagust 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);
}
最佳回答

https://developer.mozilla.org/en-US/docs/Web/Java/Reference/Global_Objects/RegExp#Syntax”rel=“noreferer”

multiline; treat beginning and end characters (^ and $) as working over multiple lines (i.e., match the beginning or end of each line (delimited by or ), not only the very beginning or end of the whole input string)

还将<代码>*放在正确位置:

"DATE:20091201T220000
SUMMARY:Dad s birthday".match(/^SUMMARY:(.*)$/gm);
//------------------------------------------------------------------^    ^
//-----------------------------------------------------------------------|
问题回答
function extractSummary(iCalContent) {
  var rx = /
SUMMARY:(.*)
/g;
  var arr = rx.exec(iCalContent);
  return arr[1]; 
}

你们需要这些变化:

  • Put the * inside the parenthesis as suggested above. Otherwise your matching group will contain only one character.

  • 查阅<代码><>>>和<代码><>。 随着全球选择的到来,它们与全面扼制的开始和结束,而不是线的开始和结束。 相反,在明确的新路线上搭桥。

  • I suppose you want the matching group (what s inside the parenthesis) rather than the full array? arr[0] is the full match (" SUMMARY:...") and the next indexes contain the group matches.

  • String.match(regexp) is supposed to return an array with the matches. In my browser it doesn t (Safari on Mac returns only the full match, not the groups), but Regexp.exec(string) works.

您的定期表达很可能是想的。

/
SUMMARY:(.*)$/g

我想要使用的一个小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小小,就是与阵列相配。

var arr = iCalContent.match(/
SUMMARY:(.*)$/g) || [""]; //could also use null for empty value
return arr[0];

这样,当你使用<代码>arr<>/code>时,你不会发现任何类型的错误。

该法典规定:

let str = "governance[string_i_want]"; 
let res = str.match(/[^governance[](.*)[^]]/g);
console.log(res);

仇恨将等于“扼杀_i_want”。 然而,在这种例子中,树脂仍是一个阵容,因此不像一种扼杀。

守则将我不希望的特性分类,使用[说明],对方括号之间的内容加以匹配,从而摘取我想要的描述!

您可以在此尝试:。 https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_match_regexp

Good luck.

<代码>(*>,代替(>)*。 后者将只反映最后一行。

此外,无需逃避<代码>:。

你们应当利用:

var arr = iCalContent.match(/^SUMMARY:(.)*$/g);
return(arr[0]);

这是你如何用javascript将iCal档案整理成文。

    function calParse(str) {

        function parse() {
            var obj = {};
            while(str.length) {
                var p = str.shift().split(":");
                var k = p.shift(), p = p.join();
                switch(k) {
                    case "BEGIN":
                        obj[p] = parse();
                        break;
                    case "END":
                        return obj;
                    default:
                        obj[k] = p;
                }
            }
            return obj;
        }
        str = str.replace(/
 /g, " ").split("
");
        return parse().VCALENDAR;
    }

    example = 
     BEGIN:VCALENDAR
 +
     VERSION:2.0
 +
     PRODID:-//hacksw/handcal//NONSGML v1.0//EN
 +
     BEGIN:VEVENT
 +
     DTSTART:19970714T170000Z
 +
     DTEND:19970715T035959Z
 +
     SUMMARY:Bastille Day Party
 +
     END:VEVENT
 +
     END:VCALENDAR
 


    cal = calParse(example);
    alert(cal.VEVENT.SUMMARY);




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签