gpt4 book ai didi

javascript - 如何在 JavaScript 中获取单词或索引后的子字符串?

转载 作者:太空宇宙 更新时间:2023-11-04 13:58:31 24 4
gpt4 key购买 nike

我的 DOM 中有以下元素。

<div id="klarna-checkout-container" style="overflow-x: hidden;">

<script type="text/javascript">
/* <![CDATA[ */
(function(w,k,i,d,n,c,l){
w[k]=w[k]||function(){(w[k].q=w[k].q||[]).push(arguments)};
l=w[k].config={
container:w.document.getElementById(i),
ORDER_URL:'https://checkout-eu.playground.klarna.com/yaco/orders/1234-fa14-4a0f-bf2d-5678',
AUTH_HEADER:'KlarnaCheckout 76c9bumqmkt8oy7wcpnr6',
LOCALE:'sv-SE',
ORDER_STATUS:'checkout_incomplete',
MERCHANT_TAC_URI:'https://demo.krokedil.se/klarnacheckout/terms/',
MERCHANT_NAME:'K500956',
GUI_OPTIONS:[],
ALLOW_SEPARATE_SHIPPING_ADDRESS:true,
PURCHASE_COUNTRY:'swe',
PURCHASE_CURRENCY:'SEK',
TESTDRIVE:true,
CHECKOUT_DOMAIN:'https://checkout-eu.playground.klarna.com',
BOOTSTRAP_SRC:'https://a.klarnacdn.net/kcoc/6788-345/checkout.bootstrap.js',
CLIENT_EVENT_HOST:'https://evt.playground.klarna.com'
};
n=d.createElement('script');
c=d.getElementById(i);
n.async=!0;
n.src=l.BOOTSTRAP_SRC;
c.appendChild(n);
try{
((w.Image && (new w.Image))||(d.createElement && d.createElement('img'))||{}).src =
l.CLIENT_EVENT_HOST + '/v1/checkout/snippet/load' +
'?sid=' + l.ORDER_URL.split('/').slice(-1) +
'&order_status=' + w.encodeURIComponent(l.ORDER_STATUS) +
'&timestamp=' + (new Date).getTime();
}catch(e){}
})(this,'_klarnaCheckout','klarna-checkout-container',document);
/* ]]&gt; */
</script>

</div>

我想获取名为 AUTH_HEADER: 的单词后的值。所以我要查找的值是 KlarnaCheckout 76c9bumqmkt8oy7wcpnr6

我尝试使用下面的代码,但它需要改进。

<script>
$(document).ready(function() {

var get_klarna_checkout_container = $('#klarna-checkout-container').text().trim();
var klarna_checkout_container_index = get_klarna_checkout_container.indexOf('AUTH_HEADER:');
var klarna_checkout_container_index_2 = get_klarna_checkout_container.substr(klarna_checkout_container_index, 289);

console.log(klarna_checkout_container_index_2);


});
</script>

上面console.log的输出是:

AUTH_HEADER:'KlarnaCheckout 76c9bumqmkt8oy7wcpnr6', LOCALE:'sv-SE', ORDER_STATUS:'checkout_incomplete', MERCHANT_TAC_URI:'https://demo.krokedil.se/klarnacheckout/terms/', MERCHANT_NAME:'K500956', GUI_OPTIONS:[], ALLOW_SEPARATE_SHIPPING_ADDRESS:true, PURCHASE_CO

我的目标是 KlarnaCheckout 76c9bumqmkt8oy7wcpnr6

我上面的代码的问题是它打印了很多字符串,而不是仅仅添加了我在 substr 方法中添加的字符。

感谢任何帮助。谢谢。

最佳答案

我走的是正则表达式路线。要查找的行是

var found = klarna_checkout_container_index_2.match(/AUTH_HEADER:'(.+)'/);

    var get_klarna_checkout_container = $('#klarna-checkout-container').text().trim();
var klarna_checkout_container_index = get_klarna_checkout_container.indexOf('AUTH_HEADER:');


var klarna_checkout_container_index_2 = get_klarna_checkout_container.substr(klarna_checkout_container_index, 289);


var found = klarna_checkout_container_index_2.match(/AUTH_HEADER:'(.+)'/);

console.log(found[1]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div id="klarna-checkout-container" style="overflow-x: hidden;">

<script type="text/javascript">
/* <![CDATA[ */
(function(w,k,i,d,n,c,l){
w[k]=w[k]||function(){(w[k].q=w[k].q||[]).push(arguments)};
l=w[k].config={
container:w.document.getElementById(i),
ORDER_URL:'https://checkout-eu.playground.klarna.com/yaco/orders/1234-fa14-4a0f-bf2d-5678',
AUTH_HEADER:'KlarnaCheckout 76c9bumqmkt8oy7wcpnr6',
LOCALE:'sv-SE',
ORDER_STATUS:'checkout_incomplete',
MERCHANT_TAC_URI:'https://demo.krokedil.se/klarnacheckout/terms/',
MERCHANT_NAME:'K500956',
GUI_OPTIONS:[],
ALLOW_SEPARATE_SHIPPING_ADDRESS:true,
PURCHASE_COUNTRY:'swe',
PURCHASE_CURRENCY:'SEK',
TESTDRIVE:true,
CHECKOUT_DOMAIN:'https://checkout-eu.playground.klarna.com',
BOOTSTRAP_SRC:'https://a.klarnacdn.net/kcoc/6788-345/checkout.bootstrap.js',
CLIENT_EVENT_HOST:'https://evt.playground.klarna.com'
};
n=d.createElement('script');
c=d.getElementById(i);
n.async=!0;
n.src=l.BOOTSTRAP_SRC;
c.appendChild(n);
try{
((w.Image && (new w.Image))||(d.createElement && d.createElement('img'))||{}).src =
l.CLIENT_EVENT_HOST + '/v1/checkout/snippet/load' +
'?sid=' + l.ORDER_URL.split('/').slice(-1) +
'&order_status=' + w.encodeURIComponent(l.ORDER_STATUS) +
'&timestamp=' + (new Date).getTime();
}catch(e){}
})(this,'_klarnaCheckout','klarna-checkout-container',document);
/* ]]&gt; */
</script>

</div>

关于javascript - 如何在 JavaScript 中获取单词或索引后的子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54757229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com