繁体   English   中英

使用JavaScript提取2个字符之间的字符串

[英]Extract string between 2 characters with javascript

我有一个看起来像这样的字符串

<iframe src="https://www.google.com/recaptcha/api2/anchor?ar=1&amp;k=6LekMVAUAAAAAPDp1Cn7YMzjZynSb9csmX5V4a9P&amp;co=aHR0cHM6Ly93d3cub21lZ2xlLmNvbTo0NDM.&amp;hl=en&amp;v=v1526884278587&amp;size=normal&amp;cb=jssxsvw1wcmm" role="presentation" scrolling="no" sandbox="allow-forms allow-popups allow-same-origin allow-scripts allow-top-navigation allow-modals allow-popups-to-escape-sandbox" style="outline: 1px solid blue;" width="304" height="78" frameborder="0"></iframe>

我希望最终输出看起来像这样

6LekMVAUAAAAAPDp1Cn7YMzjZynSb9csmX5V4a9P

基本上我想得到k=&之间的字符串

这是我到目前为止获得的JavaScript

 var str = '<iframe src="https://www.google.com/recaptcha/api2/anchor?ar=1&amp;k=6LekMVAUAAAAAPDp1Cn7YMzjZynSb9csmX5V4a9P&amp;co=aHR0cHM6Ly93d3cub21lZ2xlLmNvbTo0NDM.&amp;hl=en&amp;v=v1526884278587&amp;size=normal&amp;cb=jssxsvw1wcmm" role="presentation" scrolling="no" sandbox="allow-forms allow-popups allow-same-origin allow-scripts allow-top-navigation allow-modals allow-popups-to-escape-sandbox" style="outline: 1px solid blue;" width="304" height="78" frameborder="0"></iframe>' var string=str.substring(str.lastIndexOf("k=")+1,str.lastIndexOf("&")); console.log(string); 

但这不是我想要的输出。 谁能帮我解决这个问题? 我需要仅在javascript中使用正则表达式

以下正则表达式在k=&之间查找字符串并捕获它。

 var str = '<iframe src="https://www.google.com/recaptcha/api2/anchor?ar=1&amp;k=6LekMVAUAAAAAPDp1Cn7YMzjZynSb9csmX5V4a9P&amp;co=aHR0cHM6Ly93d3cub21lZ2xlLmNvbTo0NDM.&amp;hl=en&amp;v=v1526884278587&amp;size=normal&amp;cb=jssxsvw1wcmm" role="presentation" scrolling="no" sandbox="allow-forms allow-popups allow-same-origin allow-scripts allow-top-navigation allow-modals allow-popups-to-escape-sandbox" style="outline: 1px solid blue;" width="304" height="78" frameborder="0"></iframe>' var string=str.match(/k=([\\w]*)&/)[1] console.log(string); 

您可以尝试以下方法:

 var str = '<iframe src="https://www.google.com/recaptcha/api2/anchor?ar=1&amp;k=6LekMVAUAAAAAPDp1Cn7YMzjZynSb9csmX5V4a9P&amp;co=aHR0cHM6Ly93d3cub21lZ2xlLmNvbTo0NDM.&amp;hl=en&amp;v=v1526884278587&amp;size=normal&amp;cb=jssxsvw1wcmm" role="presentation" scrolling="no" sandbox="allow-forms allow-popups allow-same-origin allow-scripts allow-top-navigation allow-modals allow-popups-to-escape-sandbox" style="outline: 1px solid blue;" width="304" height="78" frameborder="0"></iframe>' var string=str.match(/k=([^&]*)/i)[1]; console.log(string); 

您说您要使用正则表达式,但在问题中使用了.substring

所以……这是您的更正解决方案,您还不算太远:

 var str = '<iframe src="https://www.google.com/recaptcha/api2/anchor?ar=1&amp;k=6LekMVAUAAAAAPDp1Cn7YMzjZynSb9csmX5V4a9P&amp;co=aHR0cHM6Ly93d3cub21lZ2xlLmNvbTo0NDM.&amp;hl=en&amp;v=v1526884278587&amp;size=normal&amp;cb=jssxsvw1wcmm" role="presentation" scrolling="no" sandbox="allow-forms allow-popups allow-same-origin allow-scripts allow-top-navigation allow-modals allow-popups-to-escape-sandbox" style="outline: 1px solid blue;" width="304" height="78" frameborder="0"></iframe>'; var string = str.substring(str.indexOf("k=") + 2); // Substring starts after "k=" string = string.substring(0, string.indexOf("&")); // Substring stops at the first "&" console.log(string); 

请注意,我用.indexOf()来获得第一场比赛,而不是.lastIndexOf()

希望能帮助到你。

您尝试过的内容是正确的,但是..首先您应该知道lastIndexOf返回什么。

它将返回给定字符串的开始位置,这意味着您正在寻找“ k =“。 因此它将检查“ k =”是否存在,但将返回“ k”位置。

所以总是将字符串的长度添加到lastIndexOf返回值中。.还有一个错误是,您正在检查“&”的最后一个索引。您可以使用indexOf(string,fromIndex)方法检查indexOf。对于给定索引中的给定字符串。

检查一下..

 var str = '<iframe src="https://www.google.com/recaptcha/api2/anchor?ar=1&amp;k=6LekMVAUAAAAAPDp1Cn7YMzjZynSb9csmX5V4a9P&amp;co=aHR0cHM6Ly93d3cub21lZ2xlLmNvbTo0NDM.&amp;hl=en&amp;v=v1526884278587&amp;size=normal&amp;cb=jssxsvw1wcmm" role="presentation" scrolling="no" sandbox="allow-forms allow-popups allow-same-origin allow-scripts allow-top-navigation allow-modals allow-popups-to-escape-sandbox" style="outline: 1px solid blue;" width="304" height="78" frameborder="0"></iframe>' var lookingFor = "k="; var fromIndex = str.lastIndexOf(lookingFor)+lookingFor.length; var string = str.substring(fromIndex, str.indexOf("&", fromIndex)); console.log(string); 

    var str = '<iframe src="https://www.google.com/recaptcha/api2/anchor?ar=1&amp;k=6LekMVAUAAAAAPDp1Cn7YMzjZynSb9csmX5V4a9P&amp;co=aHR0cHM6Ly93d3cub21lZ2xlLmNvbTo0NDM.&amp;hl=en&amp;v=v1526884278587&amp;size=normal&amp;cb=jssxsvw1wcmm" role="presentation" scrolling="no" sandbox="allow-forms allow-popups allow-same-origin allow-scripts allow-top-navigation allow-modals allow-popups-to-escape-sandbox" style="outline: 1px solid blue;" width="304" height="78" frameborder="0"></iframe>';
    var target  = str.split('k=')[1].split('&')[0];
    console.log(target);

暂无
暂无

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

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