Skip to content Skip to sidebar Skip to footer

.tolowercase() Only Text Except Url's?

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

Solution 1:

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

This end up like this :

$('#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);
});
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><textareaid="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>

Solution 2:

Try parsing your text to URL, and if it works, don't lower-case it:

You can see it works in a fiddle I made - https://jsfiddle.net/y1jh5w0w/.

text = text.split(' ');

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

text = text.join(' ');

Solution 3:

Why can't you use regex to find if some string is starting from https:// or http://, then don't apply toLowerCase on it.

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 matchesif (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}`);
    });
}

So, you can create a function to check if string is URL and then execute toLowerCase on non-URL strings.

Let me know if this is what you were looking for.

Updated after OP showed some snippet

I still believe regex is the solution for you

Here's the updated snippet

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);
})

As you can see, everything's got lowercased except URL.

Solution 4:

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(newRegExp(ptrn ,"g"), dictionary1[ptrn] ) );
    }
});
}, 100);
});

Post a Comment for ".tolowercase() Only Text Except Url's?"