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

时间:2018-05-30 06:54:52

标签: 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

&#13;
&#13;
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);
&#13;
&#13;
&#13;

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

5 个答案:

答案 0 :(得分:0)

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

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你可以试试这个:

&#13;
&#13;
with recursive jsonRecurse as
(
select
j.key as Path
,j.key
,j.value
from jsonb_each(to_jsonb('{
    "key1": {
        "key2": [
            {
                "key3": "test3",
                "key4": "test4"
            }
        ]
    },
    "key5": [
        {
            "key6":
            [
                {
                    "key7": "test7"
                }
            ]
        }
    ]
}'::jsonb)) j

union all

select
jr.path || '.' || jr2.Key
,jr2.key
,jr2.value
from jsonRecurse jr
       left join lateral jsonb_each(jr.value) jr2 on true
where jsonb_typeof(jr.value) = 'object'
)

select
*
from jsonRecurse;
&#13;
&#13;
&#13;

答案 2 :(得分:0)

你说你想要正则表达式,但在你的问题中使用.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()

希望它有所帮助。

答案 3 :(得分:0)

你所尝试的是正确的但是..首先你应该知道lastIndexOf返回什么..

它将返回给定字符串的起始位置,这意味着您正在寻找“k =”。所以它会检查“k =”存在但会返回“k”位置..

所以总是将字符串的长度添加到lastIndexOf返回值.. 还有一个错误,你正在检查“&amp;”的最后一个索引..你可以使用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);

答案 4 :(得分:0)

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