简体   繁体   English

使用javascript正则表达式从字符串中提取子字符串

[英]Extract substring from string using javascript regular expressions

I am new to regular expressions in Javascript. 我是Java的正则表达式的新手。

The string looks something like 字符串看起来像

Password=6)8+Ea:4n+DMtJc:W+*0>(-Y517;Persist Security Info=False;User ID=AppleTurnover;Initial Catalog=ProductDB;Data Source=Sydney

and I am trying to extract from this just the bit 我试图从中提取一点点

Password=6)8+Ea:4n+DMtJc:W+*0>(-Y517

from ths string. 从字符串。

So, I have: 所以我有:

string="`Password=6)8+Ea:4n+DMtJc:W+*0>(-Y517;Persist Security Info=False;User ID=AppleTurnover;Initial Catalog=ProductDB;Data Source=Sydney"
substring=string.match('/Password=(.*);/g');

It returns back the entire string again. 它再次返回整个字符串。 What is going wrong here? 这是怎么了?

Regex should not be wrapped in quotes. 正则表达式不应该用引号引起来。 Use [^;]+ to select anything until ; 使用[^;]+选择任何内容,直到; .

var password = string.match(/Password=([^;]+)/)[1];

 string = "`Password=6)8+Ea:4n+DMtJc:W+*0>(-Y517;Persist Security Info=False;User ID=AppleTurnover;Initial Catalog=ProductDB;Data Source=Sydney"; var password = string.match(/Password=([^;]+)/)[1]; document.write(password); 

Or lazy regex can also be used 或也可以使用惰性正则表达式

/Password=(.*?);/g
             ^

 var string = "`Password=6)8+Ea:4n+DMtJc:W+*0>(-Y517;Persist Security Info=False;User ID=AppleTurnover;Initial Catalog=ProductDB;Data Source=Sydney"; var password = string.match(/Password=(.*?);/g); document.write(password); 

It's useful to think of the underlying grammar / syntax: 考虑底层的语法/语法很有用:

string := 'Password=' password ';' 字符串:='密码=' 密码 ';' ... ...

So you want to match the non-semicolon characters. 因此,您要匹配非分号字符。

string="`Password=6)8+Ea:4n+DMtJc:W+*0>(-Y517;Persist Security Info=False;User ID=AppleTurnover;Initial Catalog=ProductDB;Data Source=Sydney"
/Password=([^;]+)/.exec(string)[0] // or [1] if you want just the password

You could Simply try this /Password.*;/ 您可以简单地尝试此/Password.*;/

So you start looking for a String with Password in the beginning, followed by whatever character, until you reach a ; 因此,您从头开始寻找带Password的字符串,然后是任意字符,直到找到;为止; .

By using the g at the end of your RegEx, you are setting it for global, so you are not only looking for the first ; 通过在RegEx末尾使用g ,您可以将其设置为global,因此,您不仅在寻找第一个; , but for every one. ,但每一个。 This might have been the reason why yours did not work. 这可能是您的笔记本无法正常工作的原因。

Avoid using potential reserved words. 避免使用潜在的保留字。

Your first regular expression worked, you just added useless quote around it. 您的第一个正则表达式有效,只是在其周围添加了无用的引号。 Removig it will works. 删除它会起作用。

var myString = "`Password=6)8+Ea:4n+DMtJc:W+*0>(-Y517;Persist Security Info=False;User ID=AppleTurnover;Initial Catalog=ProductDB;Data Source=Sydney"
var mySubstring = myString.match(/Password=(.*);Persist /g); // Remove the ' around the regex
var thePassword = mySubstring[0].replace('Password=', '');
thePassword = thePassword.replace(';Persist ', '');

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

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