RegEx Improvements
The regex can be shortened by using case-insensitive match with i
flag. We can remove the characters which are added as both lowercase and uppercase in the regex.
After removing lowercase characters regex will be as below
\?ÕÌ_|Š|Ž|À|Á|Â|Ã|Ä|Å|Æ|Ç|È|É|Ê|Ë|Ì|Í|Î|Ï|Ñ|Ò|Ó|Ô|Õ|Ö|Ø|Ù|Ú|Û|Ü|Ý|Þ|ß|ð|ÿ|_Œ‚|__|_
Here's live demo of regex
The regex can be further improved by using character class which will make the matches faster than OR conditions
\?ÕÌ_|_Œ‚|[ŠŽÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝÞßðÿ_]+
Adding +
quantifier also has positive effect on the number of steps taken to match characters when the characters in the character class are consecutive/adjacent to each other.
Here's the demo on RegEx101, without +
quantifierScreenshot and with +
quantifierscreenshot applied on the same data. Note that in these demos, PHP is selected as the steps taken to match is not shown for JavaScript. Also, the regex is different, it also contains lowercase counterparts of those special characters as i
flag is not working with PHP and don't want to apply u
(Unicode) flag as it is not supported in JavaScript.
These demos are created only to show difference when +
is applied on character class. The effect should be similar in JavaScript.
Note that the __
(two underscores) are redundant as _
is already added in character class and with g
flag it'll remove all occurrences.
Method Chaining
As replace
returns a string, any other string method can be called on it. Multiple calls to replace
can be chained.
str.replace(someRegexOrString, someString)
.replace(someOtherRegexOrString, someOtherString);
This is equivalent to
var temp = str.replace(someRegexOrString, someString);
var result = temp.replace(someOtherRegexOrString, someOtherString);
Replacing HTML
jQuery html()
accepts a function which will receive the current innerHTML
of the element on which the method is called as parameter and replaces the returned content to the element.
The code can be written as
$('.rte').html(function(index, currentHTML) {
return doSomeOperationOn(currentHTML);
});
Complete Code
With above changes, the code will be
$(document).ready(function() {
var regex = /\?ÕÌ_|_Œ‚|[ŠŽÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝÞßðÿ_]+/gi;
$('.rte').html(function(i, oldHTML) {
return oldHTML.replace(regex, ' ')
.replace(/[^\x00-\x7F]|\?/g, '');
});
});
$(document).ready(function() {
is more readable than $(function() {
. So, you may also consider using more expressive form.