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 badges3 Answers
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
版权声明:本文标题:regex - javascript regular expression to separate string at first equal sign - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742309712a2450621.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论