.tolowercase()仅URL以外的文本? [英] .tolowercase() only text except URL's?

查看:115
本文介绍了.tolowercase()仅URL以外的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been working on this script for Greasemonkey. Problem is .tolowercase() obviously it's making anything uppercase to lowercase, which breaks URL's. I've looked into .startswith() and .endswith() as possible solutions, such as starts with http and https; maybe prefix detection some how 'http*'? Anyways, this is the code below, thanks in advance:

// ==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.

This is the HTML before the code is ran on it.

<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外的文本应忽略大小写,但如果检测到自己的单词,请仍然替换文本.

edit: its important that the text except urls is to have upper or lower case ignored yet still .replace text if the words them selves are detected.

推荐答案

因此,我尝试将库存位置,URL和重新添加位置进行组合.

So I've tried with a combination of stocking the position, the url and re-appending it.

最终结果如下:

$('#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>

这篇关于.tolowercase()仅URL以外的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆