简体   繁体   中英

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"

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

and the following string should become: "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. 3) You want to extract entire name after "Customer" string. 4) Name is max two words longs. 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'

Please remember to trim main string before applying regEx operation.

Will try to add jsfiddle here. https://jsfiddle.net/k9oo7wLz/1/

Use the m flag to search in multiline mode

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:

    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. 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.

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. 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. If you try to copy this fiddle, you have to be sure to import MooTools to make it work in your own fiddle.

You can use the following regex:

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

JSFiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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