gpt4 book ai didi

javascript - .tolowercase() 只有文本,URL 除外?

转载 作者:行者123 更新时间:2023-11-30 15:23:43 24 4
gpt4 key购买 nike

我一直在为 Greasemonkey 编写这个脚本。问题是 .tolowercase() 显然它会将任何大写字母变成小写字母,这会破坏 URL。我研究了 .startswith() 和 .endswith() 作为可能的解决方案,例如以 http 和 https 开头;也许前缀检测有些如何'http *'?无论如何,这是下面的代码,在此先感谢:

// ==UserScript==
// @name twitter intent
// @namespace random
// @description twitter intent popupwindow text replace
// @include https://twitter.com/intent/*
// @version 1
// @grant none
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js
// ==/UserScript==


$('#status').each(function() {
var text = $(this).text();
$(this).text(text.toLowerCase()
.replace('... ', ' ... ')
.replace('health ', '#health ')
.replace('video', 'Video')
.replace('emergency', '#emergency')
.replace('climate change', '#climatechange')
.replace('climate ', '#climate ')
);
});

这是代码运行之前的 HTML。

<textarea id="status" name="status" required="" autofocus="" aria-required="true" aria-describedby="post-error char-count">EPA shuts down program helping states adjust to climate change http://hill.cm/FH2iWpq</textarea>

编辑:重要的是,除了 url 之外的文本要忽略大写或小写字母,但如果检测到单词本身,则仍然替换文本。

最佳答案

所以我尝试结合存储位置、url 和重新附加它。

结果是这样的:

$('#status').each(function() {
var text = $(this).text();
var n = text.indexOf('http');
var url = text.match('http(s?):\/\/[^<\s]*');
if(url){
text = text.replace(url[0],'')
}
text = text.toLowerCase()
.replace('... ', ' ... ')
.replace('health ', '#health ')
.replace('video', 'Video')
.replace('emergency', '#emergency')
.replace('climate change', '#climatechange')
.replace('climate ', '#climate ')
if(url){
text = text.slice(0,n) + url[0] + text.slice(n);
}
$(this).text(text);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="status" name="status" required="" autofocus="" aria-required="true" aria-describedby="post-error char-count">EPA shuts down program helping states adjust to climate change http://hill.cm/FH2iWpq</textarea>

关于javascript - .tolowercase() 只有文本,URL 除外?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43303603/

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