如何从 Html5-QRCode 输出中去除“http://”

问题描述 投票:0回答:1

我正在使用 https://github.com/mebjas/html5-qrcode 中的代码将相机扫描转换为文本字段,并注意到扫描的输出添加了“http://”作为前缀。 如何从输出中去除“http://”?下面的实际代码。 (“...”是忽略代码中所有与问题无关的部分。) 谢谢。

QR Code: <input type="text" id="QRCode" name="QRCode" value="Please scan the QR code" disabled="disabled"<br/><br/>
...
let outputContainer = document.getElementById('QRCode');
var html5QrcodeScanner = new Html5QrcodeScanner(
    "qr-reader", { fps: 10, qrbox: 250 });

function onScanSuccess(decodedText, decodedResult) {
    if (decodedText !== lastResult) {
        // Handle on success condition with the decoded text or result.
        console.log(`Scan result: ${decodedText}`, decodedResult);
        outputContainer.value = decodedText;
    }
}

html5QrcodeScanner.render(onScanSuccess);

尝试编辑 .js 文件但没有成功,因为对“URL”或“http”的引用不多——对“http://”的引用为零。搜索 GitHub 项目页面没有结果。

javascript html qr-code
1个回答
0
投票

此代码将从字符串的开头删除

http://
https://

const url = 'https://www.example.com';

// remove http:// or https:// from the beginning of the string
const strippedUrl = url.replace(/^https?:\/\//, '');

console.log(strippedUrl); // "www.example.com"

集成到您的代码中

function onScanSuccess(decodedText, decodedResult) {
    if (decodedText !== lastResult) {
        decodedText = decodedText.replace(/^https?:\/\//, '');
        // Handle on success condition with the decoded text or result.
        console.log(`Scan result: ${decodedText}`, decodedResult);
        outputContainer.value = decodedText;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.