简体   繁体   中英

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 &

this is the javascript I got so 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.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

Following regex looks for string between k= and & and captures it.

 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.

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() .

Hope it helps.

what you have tried is correct but.. first you should know what lastIndexOf returns..

it will return the start position of given string, it means you are looking for "k=". so it will check for the "k=" existence but will return the "k" position..

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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