admin管理员组

文章数量:1335620

Here is a string I am trying to recognize in a text file. The text file has a specific field I need to extract in each line. Ex:

Field1 = This is the content for field 1
Field2 = This is the content for field 2
Field3 = This is the content for field 3, which is = to 4445

Assume I want to extract the content of field3 after its first "=" sign. How can I get that content "This is the content for field 3, which is = to 4445" including the second "=" sign but not the first one. I am just getting into regular expressions, so I have not much experience. Here is what I tried:

=.*

but this will print the first "=" sign and everything after it. I want to omit the first = sign.

Likewise, what would be the regexp to get "Field3" without the first "=" sign? I know I could split the line based on the first equal sign but I really need to do it with regexps. Thank you

Here is a string I am trying to recognize in a text file. The text file has a specific field I need to extract in each line. Ex:

Field1 = This is the content for field 1
Field2 = This is the content for field 2
Field3 = This is the content for field 3, which is = to 4445

Assume I want to extract the content of field3 after its first "=" sign. How can I get that content "This is the content for field 3, which is = to 4445" including the second "=" sign but not the first one. I am just getting into regular expressions, so I have not much experience. Here is what I tried:

=.*

but this will print the first "=" sign and everything after it. I want to omit the first = sign.

Likewise, what would be the regexp to get "Field3" without the first "=" sign? I know I could split the line based on the first equal sign but I really need to do it with regexps. Thank you

Share Improve this question asked Sep 17, 2013 at 18:14 JohnnyLooJohnnyLoo 9273 gold badges12 silver badges25 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5
.*?=\s*(.*)

Debuggex Demo

  • . means all characters except newlines
  • + means one more of that character
  • () denotes a capture group
  • * means zero or more
  • \s means white space
  • *? Zero or more a's (lazy)

Extract it out of the capture group. It should be in your first capture group. Remember the entire array is stored in the 0 indexed of match.

var match = myRegexp.exec(input);
alert(match[1]);

please read more about regex here and use debugexx. to experiment.

You will have to use a capture group. Something which will be a bit like that:

str.match(/=\s*(.*)$/)[1];

Or you do:

res = str.match(/=\s*(.*)$/);
console.log(res[1]);

If you want a 'safer' regex, you might try this:

str.match(/^[^=]+=\s*(.*)$/);

^ matches the beginning of line;

[^=]+ matches any characters except the equal sign;

= will then match the first equal sign;

(.*)$ will capture everything after the first equal sign until the end of the string.

To match Field3 only you may use lookahead assertion:

str.match(/Field3(?=\s*=)/);

This shall match Field3 if it's followed by the equal sign, but won't include the equal sign in the match string.

本文标签: regexjavascript regular expression to separate string at first equal signStack Overflow