简体   繁体   English

多行字符串中间的正则表达式?

[英]Regular expression for middle of multiline string?

I'm trying to extract first name and/or last name from a multiline string and can't get the regular expression to work. 我正在尝试从多行字符串中提取名字和/或姓氏,并且无法使用正则表达式。

The string sometimes only contains a first name and sometimes a last name also exists. 该字符串有时仅包含一个名字,有时还包含一个姓氏。

The following string should become: "John Doe" 以下字符串应变为:“ John Doe”

" 
            <a>Customer: John Doe</a>
        "

and the following string should become: "John" 并且以下字符串应变为:“ John”

" 
            <a>Customer: John</a>
        "

OK, I am considering the following assumptions in your string and this answer is based on these assumptions: 好的,我正在考虑您的字符串中的以下假设,并且此答案基于这些假设:

1) Always string contain this pattern Cutomer: SOME_NAME 2) SOME_NAME will always contain some characters but not special characters. 1)始终包含此模式的字符串剪切者:SOME_NAME 2)SOME_NAME将始终包含一些字符,但不包含特殊字符。 3) You want to extract entire name after "Customer" string. 3)您想在“客户”字符串之后提取整个名称。 4) Name is max two words longs. 4)名称最多为两个字长。 If name can contain any number of words some modification is required. 如果名称可以包含任意数量的单词,则需要进行一些修改。

Solution: 解:

var regEx = /.*Customer:\s?(\w*\s?\w*)\s?.*$/
var myStr = "<a>Customer: John Doe</a>";
var output = myStr.replace(regEx,'$1');

Now, output contains string 'John Doe' 现在,输出包含字符串“ John Doe”

Please remember to trim main string before applying regEx operation. 请记住在应用regEx操作之前先修剪主字符串。

Will try to add jsfiddle here. 将尝试在此处添加jsfiddle。 https://jsfiddle.net/k9oo7wLz/1/ https://jsfiddle.net/k9oo7wLz/1/

Use the m flag to search in multiline mode 使用m标志在多行模式下搜索

var regex = /Customer:\s*(\w+)\s*(\w*)/gmi;
var result = regex.exec(inputStr);
var firstName = "";
var lastName = "";
if (result) {
  firstName = result[1];
  if (result.length === 3)
    lastName = result[2];
}

Here's an improved version of Dummy's answer above: 这是上面Dummy答案的改进版本:

    var text = yourInputText;
    var regex = /Customer:\s*(\w+)\s(\w*)/gm;
    var result = regex.exec(text);
    var theValue;
    if (result) {
      theValue = "First Name: " + result[1];
      if ( result[2] != "" ) {
        theValue += "<br/>Last Name: " + result[2];
      }
    }

You have to test the value of result[2] not just test the length of the result array. 您必须测试result[2]的值, result[2]不仅仅是测试结果数组的长度。 This is because you have two capture groups in the regex. 这是因为您在正则表达式中有两个捕获组。 There will always be a length of 3 in this case, but the third element will be empty. 在这种情况下,长度始终为3,但是第三个元素为空。

I can't think of one single regex that would not require you to do some post capture logic. 我想不出一个不需要您执行一些后期捕获逻辑的正则表达式。 You either have to have one big capture group that would gets both types of strings or two capture groups. 您要么必须有一个大型捕获组才能同时获取两种类型的字符串,要么需要两个捕获组。 You just have to make a decision about which logic works best for you 您只需要决定哪种逻辑最适合您

I've set this up in a jsfiddle that jams this logic into a function and attaches it to the click of two divs that contain your example strings, try it out. 我已经在jsfiddle中进行了设置 ,将该逻辑塞入函数并将其附加到包含示例字符串的两个div的单击上,然后尝试一下。 The $ functions that I'm using in the fiddle are just from MooTools to give me access to the DOM, don't be distracted by them. 我在小提琴中使用的$函数只是来自MooTools ,使我可以访问DOM,请不要被它们分散注意力。 If you try to copy this fiddle, you have to be sure to import MooTools to make it work in your own fiddle. 如果您尝试复制此小提琴,则必须确保导入MooTools以使其在您自己的小提琴中起作用。

You can use the following regex: 您可以使用以下正则表达式:

"[\s\S]*?<a>\w+: (.+)<\/a>[\s\S]*?"

JSFiddle JSFiddle

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

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