简体   繁体   English

Javascript —如果StartsWith查找并剪切

[英]Javascript — Find And Cut if StartsWith

I need to see if the first line of text starts with a By and if true, cut the whole line and store it in a variable and remove all blank lines until the next paragraph starts. 我需要查看文本的第一行是否以“ By开头,如果为true,则剪切整行并将其存储在变量中,并删除所有空行,直到下一段开始为止。 The method to find By needs to be case insensitive and it may have some preceding white spaces too. 查找“ By ”的方法必须不区分大小写,并且也可能有一些前面的空格。 And do nothing if the first line isn't starting with a By . 如果第一行不是以By开头,则不do nothing

var findBy = 'By Bob';
if (findBy.startsWith('By ')) {
findBy = copyBy;
findBy.split("\n").slice(1).join("\n");
}
var pasteBy = copyBy; 

Let me rephrase myself: Find if first line starts with By If it does, save the entire line containing By in a variable then delete it. 让我自己改一下:查找第一行是否以“ By开头,如果包含,则将包含“ By的整行保存在变量中,然后将其删除。

Adjustment ... 调整 ...

function removeBy(textArray) {
  var capture = false;
  rebuild = [];
  for (var i=0,len=textArray.length; i<len; i++) {
    var s = textArray[i].trim();
    if (s.substring(0,3).toUpperCase()==="BY ") {
      capture = true;
    } else if (capture && s!=="") {
      capture = false;
    }

    if (capture) {
      rebuild.push(s);
    }
  }
  return rebuild;
}

This function assumes you are sending an array of strings and returns a stripped array. 此函数假定您要发送一个字符串数组,并返回一个剥离的数组。

var answer = removeBy(["By Bob", "", "", "This is the result"]);
// answer = ["By Bob"];

Fiddle: http://jsfiddle.net/rfornal/ojL72L8b/ 小提琴: http : //jsfiddle.net/rfornal/ojL72L8b/

If the incoming data is separated by line-breaks, you can use the .strip() function to make the textArray ; 如果输入的数据由换行符分隔,则可以使用.strip()函数制作textArray ; In reverse, the rebuild returned can be put back together with answer.join("\\n"); 相反,可以将返回的rebuildanswer.join("\\n");放在一起answer.join("\\n");

UPDATE UPDATE

Based on comments, changed substring to (0,3) and compared against "BY " (space) to ONLY watch for BY not something like BYA. 根据注释,将substring更改为(0,3)并与"BY " (空格)进行比较,仅注意BY而不是BYA。

Updated Fiddle: http://jsfiddle.net/rfornal/b5gvk48c/1/ 更新的小提琴: http : //jsfiddle.net/rfornal/b5gvk48c/1/

If I understand well your requirements, you could use this regex. 如果我很了解您的要求,则可以使用此正则表达式。
(maybe not the most powerful, I'm not a regex specialist at all) (也许不是最强大的,我根本不是正则表达式专家)

/^(\s\bby\b|\bby\b).*$/gmi
  • Little walkthrough starting by last modifiers : 由last修饰符开始的小演练:
    g global , it won't return at first occurence. g global ,它不会在第一次出现时返回。
    m multiline , ^ and $ will match start/end of line, instead of string m multiline^$将匹配行的开始/结束,而不是字符串
    i insensitive , I bet you got it i 麻木了 ,我敢打赌你明白了
  • Now, back to the beginning. 现在,回到开始。
    ^ will search at start of the string ^将在字符串的开头搜索
    (x|y) is a capturing group with two alternatives (x or y) (x|y)是具有两个替代项(x或y)的捕获组
    \\s\\ is any white-space (\\r\\n\\t\\f) \\s\\是任何空格(\\ r \\ n \\ t \\ f)
    \\bword\\b is a word boundary , eg it won't catch "word s " \\bword\\b是一个单词边界 ,例如它将不会捕获“ word s
    .* matches any character (except newline), until .*匹配任何字符(换行符除外),直到
    $ end of string/line here $这里的字符串/行的结尾

 //those should be found var text = "by test 1 \\n" + " by test 2 \\n" + "By test 3 \\n" + " By test 4 \\n" + //Those should not "test By 5 \\n" + "test by 6 \\n" + "Bya test 7 \\n" + "bya test 8 \\n" + " Bya test 9 \\n" + " bya test 10 \\n"; var matchingLines = text.match(/^(\\s\\bby\\b|\\bby\\b).*$/gmi); document.querySelector('p').textContent = "|" + matchingLines.join("|") + "|"; 
 <p/> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM