Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript中的正则表达式-以开头,以结尾_Javascript_Regex - Fatal编程技术网

JavaScript中的正则表达式-以开头,以结尾

JavaScript中的正则表达式-以开头,以结尾,javascript,regex,Javascript,Regex,我想在Javascript中使用正则表达式提取字符串 比如说 StudentName nameX = John; 或 我只想提取“nameX”,这是我到目前为止尝试过的 var name = allName.match("StudentName(.*);|StudentName(.*)="); 我得到的是:“nameX=John”,但我不会只得到“nameX”。试试这种非贪婪模式 var name = allName.match("StudentName\\s*(.*?)\\s*[=;]"

我想在Javascript中使用正则表达式提取字符串 比如说

StudentName nameX = John; 

我只想提取“nameX”,这是我到目前为止尝试过的

var name = allName.match("StudentName(.*);|StudentName(.*)=");

我得到的是:“nameX=John”,但我不会只得到“nameX”。

试试这种非贪婪模式

var name = allName.match("StudentName\\s*(.*?)\\s*[=;]");

试试这种非贪婪模式

var name = allName.match("StudentName\\s*(.*?)\\s*[=;]");

如果在空白处拆分,则索引1处的第二个匹配项应包含名称

var name = allName.split(/[ ;]/g)[1];

如果在空白处拆分,则索引1处的第二个匹配项应包含名称

var name = allName.split(/[ ;]/g)[1];

match

match(/StudentName\s+(\w+)/)[1]

请参见。​​

匹配中使用正则表达式模式

match(/StudentName\s+(\w+)/)[1]

请参见。​​

谢谢,行得通。有没有办法找到所有的物体?例如,如果您有StudentName nameX;学生姓名;学生姓名nameZ;你能找到所有的名字吗?Thanks@kkDev-要查找所有事件,请使用
re=new RegExp()
,同时使用
match=re.exec()
条件循环。谷歌搜索:)我会试试看。。。谢谢,谢谢,行得通。有没有办法找到所有的物体?例如,如果您有StudentName nameX;学生姓名;学生姓名nameZ;你能找到所有的名字吗?Thanks@kkDev-要查找所有事件,请使用
re=new RegExp()
,同时使用
match=re.exec()
条件循环。谷歌搜索:)我会试试看。。。谢谢