RegEx to replace html entities - javascript

all. I'm looking for a way to replace the bullet character in Greasemonkey. I assume a Regular Expression will do the trick, but I'm not as well-versed in it as many of you.
For example, "SampleSite.com • Page Title" becoming "SampleSite.com Page Title". The issue is that the character has already been parsed by the time Greasemonkey has gotten to it, and I don't know how to make it recognize the symbol.
I've tried these so far, but they haven't worked:
newTitle = document.title.replace(/•/g, "");
newTitle = document.title.replace("•", ""); //just for grins, but didn't work anyway

You can do something like this, if Malvolio's solution isn't working
newTitle = document.title.replace(/\&bull\;/g, '');
newTitle = newTitle.replace(/([^a-zA-Z0-9-_\s\/\\\(\)\'\"\&\+\.]+)/g, '');

document.title = document.title.replace(/•/g, "");
works for me.

HTML entities defined by code between & and ; replace · or · (probably in your case) based on page encoding. Better encode html before using RegEx to replace.

Related

Use jQuery to Auto Escape characters from a var

First, I'm not sure I've titled my question properly. Please feel free to correct me if needed.
My Issue:
I've created a variable, in jQuery called var siteTitle. This variable is available for other .js files to use and then get passed back to the .html page.
It all works great and there are no issues except when the var siteTitle will contain certain characters that need to be escaped. (quote, single quote, and ampersand to be specific)
What I would like to do is to use a bit of jQuery that would search a particular dom element and see if it is using any of those characters and then automatically escape them.
I've searched for some similar functions and can not seem to find exactly what I need ... the closet idea I have seen is something like this. Its not exactly what I need but it is something like what I am looking for.
pathto: function(path, file) {
var rtrim = function(str, list) {
var charlist = !list ? 's\xA0': (list + '').replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1');
var re = new RegExp('[' + charlist + ']+$', 'g');
return (str + '').replace(re, '');
};
So, I am trying to write a function that will automatically convert those characters to be escaped or their html equivalent.
So, if the var siteTitle is used in a dom element like this:
<h1 class="titleText">' + siteTitle + '</h1>
I need to be able to make sure that any characters get escaped in that element.
Here is a jsFiddle that shows exactly what I am trying to do ...
https://jsfiddle.net/bbyrdhouse/5jb2fdsr/1/
Any help is greatly appreciated.
Since you're using jquery, use the .text() function to set the value into your HTML. It'll escape it appropriately.
var siteTitle = 'My Site "Title"';
$my('.titleText').text(siteTitle);
Also, in your fiddle, the siteTitle variable is not what you think it is, because the 2nd quotation closes that value since it's not yet escaped. I wrapped it in single quotes in my example.
Updated fiddle

Regular expression for selecting part of string and excluding another part of string

I have one string as variable and I want to use str.replace to replace each search string hit with <span style="..."> + search string + </span>. The problem is that I do this in loop in which the search string changes and some times the search string is similar to span or style. When this happens the result could be something like this <span <span style="...">style</span>="...">search string</span> which is catastrophic. So I need someone to help my by creating regular expression which selects the search string and excludes <span style="..."> and <span> or give me an idea how to solve this.
Here is parth from my code which needs to be changed:
if ($("#search_criteria option:selected").text() == lng("note_text_txt", "note_text_txt") && $("#search_note").val().trim() != "")
{
var SearchArr = $("#search_note").val().split(" ");
for (i = 0; i < SearchArr.length;i++)
{
data["text"] = data["text"].replace(RegExp(SearchArr[i], 'g'), "<span style='color:red;'>" + SearchArr[i] + "</span>");
}
}
var SearchArr has words which need to be found in data["text"] and replaced.
I'm posting this answer to provide you a regex to match and get content between your <span>...</span>.
But, I'm not sure what exactly you want to do. You can post some comments to help me understand your fully needs so I can update the answer to give an accurate post.
For the moment, I leave you the regex and a demo below.
Matching and get content regex
<span.*?>(.*?)<\/span>
Here you can see a working example
On the other hand, you can use another option for replace the content.
Replace content regex
(<span.*?>)(.*?)(<\/span>)
Here you have the working demo
Hope to help.
If you are already using JQuery, you could replace the actual tag contents using selectors of the library.
I would think like this:
$(data['text']).find('*:contains(SearchArr[i])').text(function(index, text) {
return text.replace(SearchArr[i], '<span>');
});
Take this as an idea and example. In an actual use you should specify better the elements above selected by *. The 'all' selector combined with :contains would take a big hit on the performance.
Edit:
It will work with your plain text just as well. If you provide valid HTML for a constructor of JQuery object, the DOM element packed in JQuery object gets created and you can operate on it in any way you would on any one selected from the document. Have a look at the following example: http://jsfiddle.net/Vk7N3/1/

str replace all in Javascript

I am trying to some some urls throught javascript where some replacement of urls needs to be done. I have a textarea with some URLs example given below:
http://mywebsite.com/preview.aspx?mode=desktop&url=http://mywebsite.com/post.aspx?id=44&content=1
http://mywebsite.com/preview.aspx?mode=desktop&url=http://mywebsite.com/post.aspx?id=44&content=2
http://mywebsite.com/preview.aspx?mode=desktop&url=http://mywebsite.com/post.aspx?id=44&content=3
http://mywebsite.com/preview.aspx?mode=desktop&url=http://mywebsite.com/post.aspx?id=44&content=3
Now what i am trying to do is replacing http://mywebsite.com/preview.aspx?mode=desktop&url= with spaces.
I have tried using str.replace() but it is replacing only first occurence of that url.
I have also tried with Global variable g the query i have used is
str_replace(\http://mywebsite.com/preview.aspx?mode=desktop&url=/g,'');
But its not working So can anyone tell me how i can do that ?
I want the output of the textarea like:
http://mywebsite.com/post.aspx?id=44&content=1
http://mywebsite.com/post.aspx?id=44&content=2
http://mywebsite.com/post.aspx?id=44&content=3
http://mywebsite.com/post.aspx?id=44&content=4
I believe that your biggest issue is that your regex syntax is incorrect. Try this:
Imagine that var s is equal the the value of your textarea.
s.replace(/http\:\/\/mywebsite\.com\/preview.aspx\?mode\=desktop\&url\=/g, '');
The issue you were having was improper delimiters and unescaped reserved symbols.
Though Javascript has some of its own regex idiosyncrasies, the issues here were related to basic regex, you might find these resources useful:
http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/
http://regexpal.com
try this.
var string = document.getElementById('textareaidhere');
string.replace(/http:\/\/mywebsite\.com\/preview\.aspxmode=desktop&url=/g, '');
JSFiddle here

JavaScript Regex expression to extract data from html comment

There is a html comment with an Id that I need to extract. The comment is on a div, which is not hard to get using the JQuery $ operator. But the correct RegEx string I need I have not been able to figure out. This is the comment:
<!-- sid=FFKK12H1 -->
And I need a JS variable that has the string "FFKK12H1" assigned to. What is the correct syntax/expression to use? thanks!
EDIT:
I forgot a very important piece of information: The code needs to work on IE7. Unfortunately this is the browser my company allows us to use, and none of the proposed solutions work there so far. Any other thoughs?
The regular expression would be: /<!-- sid=(.+?) -->/i:
var str = '<!-- sid=FFKK12H1 -->';
console.log(str.match(/<!-- sid=(.+?) -->/i)[1]);
var content = $('#comment-containg-div').html();
var regex = /<!--\s*sid=([\x00-\x7F]+)\s*-->/;
var matches = regex.exec(content);
console.log(matches);
The regex here is a amalgamted answer that includes all of the suggestions that other people on the page have made, it seems like it would be the safest to use.
var my_id = my_string.replace(/.*<!-- sid=(.*) -->.*/gi, '$1');
Example
http://jsfiddle.net/YTdKQ/

Prototype.js or other js function to decode html entities

I am looking for a prototype.js or other js function to decode html encoded entities. I am using 1.6.1 of Prototype.js and unescapeHTML does not work on French encoded characters. I believe from what I read, that is only works on a few select entities.
Can someone point me in the right direction on how I would do something like this with javascript? I would normally be able to use the .text() with jQuery, but right now the main library used is Prototype.
Thanks.
How about this:
function decode(str) {
var div = document.createElement('div');
div.innerHTML = str;
return div.innerHTML;
}
Doesn't return & properly but works fine for french ones. Updated fiddle:http://jsfiddle.net/mrchief/MRqnQ/3/
The built-in decodeURI function may be what you're looking for. It ignores "special" characters, but will turn an arbitrary URI-encoded string into what it represents.
Example:
encodeURI("Déjà vu") = "D%C3%A9j%C3%A0%20vu"
decodeURI("D%C3%A9j%C3%A0%20vu") = "Déjà vu"
An alternative may be to use a regular expression.
Try using that :
http://phpjs.org/functions/htmlentities:425

Categories