简体   繁体   English

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

[英]Extract string between 2 characters with javascript

I have a string that looks like this 我有一个看起来像这样的字符串

<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>

I want the final output to look like this 我希望最终输出看起来像这样

6LekMVAUAAAAAPDp1Cn7YMzjZynSb9csmX5V4a9P

basically I want to get the string between k= and & 基本上我想得到k=&之间的字符串

this is the javascript I got so far 这是我到目前为止获得的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); 

but it's not the desired output I want. 但这不是我想要的输出。 Can anyone help me fix this? 谁能帮我解决这个问题? I need it to be regex only in javascript 我需要仅在javascript中使用正则表达式

Following regex looks for string between k= and & and captures it. 以下正则表达式在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); 

You could try this: 您可以尝试以下方法:

 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); 

You say you want regex but used .substring in your question. 您说您要使用正则表达式,但在问题中使用了.substring

So… Here is your solution with corrections, you weren't that far: 所以……这是您的更正解决方案,您还不算太远:

 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); 

Note that I used .indexOf() to get the first match instead of .lastIndexOf() . 请注意,我用.indexOf()来获得第一场比赛,而不是.lastIndexOf()

Hope it helps. 希望能帮助到你。

what you have tried is correct but.. first you should know what lastIndexOf returns.. 您尝试过的内容是正确的,但是..首先您应该知道lastIndexOf返回什么。

it will return the start position of given string, it means you are looking for "k=". 它将返回给定字符串的开始位置,这意味着您正在寻找“ k =“。 so it will check for the "k=" existence but will return the "k" position.. 因此它将检查“ k =”是否存在,但将返回“ k”位置。

so always add length of the string to the lastIndexOf return value.. and one more mistake is, you are checking for last index of "&".. you can check indexOf using indexOf(string,fromIndex) method.. that will start searching for the given string from given index.. 所以总是将字符串的长度添加到lastIndexOf返回值中。.还有一个错误是,您正在检查“&”的最后一个索引。您可以使用indexOf(string,fromIndex)方法检查indexOf。对于给定索引中的给定字符串。

check this.. 检查一下..

 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