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