2017-04-09 5 views
1

私はGreasemonkey用のこのスクリプトを作成しています。問題は.tolowercase()です。明らかに、大文字か小文字かを問わず、URLを壊してしまいます。私はhttpとhttpsで始まるような、可能な解決策として.startswith()と.endswith()を調べました。たぶん接頭辞の検出にいくつかの 'http *'?とにかく、これは以下のコードです:.tolowercase()URL以外のテキストのみ?

// ==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を除き、テキスト、単語、それらの自分が検出された場合は、まだ、まだテキストを.replace無視上部または下部ケースを持つことであることをその重要。小文字にない

+0

、あなたはそれがあるとしてURLが滞在したいですか? –

+0

あなたは正しいです – computerguy

+0

'$( '#status')。それぞれのIDは定義によって一意でなければならないので意味がありませんまた、テキストまたは ''タグのurlですか?[mcve] – charlietfl

答えて

1

私はその位置を保管し、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>

+0

皆様とその努力のおかげですが、この答えは最適で、私は楽しみにしています誰も私以外の誰かに恩恵を受けている人に返信する – computerguy

+0

'.replace( '気候変動'、 '#climatechange')を使用したときに、気になる問題をどのようにして取得したのですか?#climatechangeとlinkを空にしました – computerguy

+0

' var url = text.match( 'http(s?):\/\/[^ <\ s] *');と動作するようですが、それが最良の解決策であるかどうかはわかりません。私はhttpの前にスペースを追加しました。 – computerguy

1

は、URLにあなたのテキストを解析してみて、それが動作するかどうか、実行します。

あなたはそれが私が作ったバイオリンで働く見ることができます - https://jsfiddle.net/y1jh5w0w/。あなたには、いくつかの文字列がhttps://またはhttp://から開始されている場合を見つけるために正規表現を使用することはできませんなぜ

text = text.split(' '); 

text.forEach(function (value, index) { 
    try { 
     var url = new URL(value); 
    } catch (ex) { 
     text[index] = value.toLowerCase(); 
    } 
}); 

text = text.join(' '); 
+0

OK、これを試してください。 – Matansh

+0

私は上記のように複数の単語を置き換えたいと思っていますが、具体的な文ではありません。 – computerguy

+0

大文字か小文字かを問わずテキストを無視するようにしてURLを無視するだけです – computerguy

0

、それにtoLowerCaseを適用していません。

const regex = /^(https:\/\/)/g; 
 
const str = `https://www.FaceBook.com`; 
 
let m; 
 

 
while ((m = regex.exec(str)) !== null) { 
 
    // This is necessary to avoid infinite loops with zero-width matches 
 
    if (m.index === regex.lastIndex) { 
 
     regex.lastIndex++; 
 
    } 
 
    
 
    // The result can be accessed through the `m`-variable. 
 
    m.forEach((match, groupIndex) => { 
 
     console.log(`Found match, group ${groupIndex}: ${match}`); 
 
    }); 
 
}

だから、あなたは文字列がURLであるかどうかを確認するための関数を作成し、非URL文字列にtoLowerCaseを実行することができます。

あなたが探していたものかどうか教えてください。 OPは、いくつかのスニペットを示した後

が更新スニペット

var str = 'EPA shuts down program helping states adjust to climate change http://hill.cm/FH2iWpq Which url is lowercased dummy text'; 
 
var regex = /^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/g; 
 
str.split(" ").forEach(function (obj) { 
 
    if(obj.match(regex) === null) { 
 
    obj = obj.toLowerCase(); 
 
    } 
 
    console.log(obj); 
 
})

あなたが見ることができるように

が、私はまだ正規表現は、ここでは、

のためのソリューションであると信じています更新しました、URLを除いてすべてが小文字になります。

+0

私はそれを稼働させようとしていますが、運が全くありません – computerguy

+0

iveが追加されましたこれは私のコードの下に、私のコードで.tolowercase()と一緒に働くようです。 – computerguy

+0

大文字か小文字かを問わず、テキストを無視するという重要な点を追加してURLSを無視することができます。つまり、大文字でも大文字でもregaurdlessを意味します。 – computerguy

0
var dictionary1= { 
    " A":" b", 
    " B":" b", 
    " C":" C", 
}; 

jQuery(document).ready(function() { 
    setTimeout(function() { 
$("#status").each(function(){ 
    for(var ptrn in dictionary1){ 
     $(this).text($(this).text().replace(new RegExp(ptrn ,"g"), dictionary1[ptrn])); 
    } 
}); 
}, 100); 
}); 
関連する問題