Regex to remove a whitespace from href link [duplicate] - javascript

This question already has answers here:
Remove ALL white spaces from text
(14 answers)
Closed 6 years ago.
Can some one help me to create a regular expression in Javascript to remove a whitespace from href link and replace whitespace to hyphen to in my content?
For example:
<a class="card" href=http://www.eee.com/sffsd/sdfs/Aks's Reb outsider/4234234234324>
it should convert it into
<a class="card" href=http://www.eee.com/sffsd/sdfs/Aks's-Reb-outsider/4234234234324>

A couple of things, like I said in the comment, replacing the spaces with dashes should be as easy as:
link.href = link.href.replace(/ /g, '-');
//or in php:
$href = preg_replace('/ /', '-', $href);
(JS only): using the g flag ensures the entire string is searched for spaces, and replaces them all with dashes.
The better question to ask is: how did the spaces get there in the first place?
My first port of call would be to look at the code generating the markup, and fix the problem there. You shouldn't be writing code fixing the output of code that is broken. Fix the bug, don't accommodate it.
Arguably, the URL's should be properly escaped, rather tan using regex, the URI should've been passed through a function like encodeURI to convert all spaces to %20 etc...

use this regex format
link.href = link.href.replace(/\s/g, '-');

Related

Strip tags from a string - HTML5 [duplicate]

This question already has answers here:
Remove HTML Tags in Javascript with Regex
(13 answers)
Closed 7 years ago.
The following string is valid HTML in HTM5, even with the > in the attribute:
'<span src="whatever.png" data-info="hello>there">text</span>';
Almost all solutions proposed before use a variant of:
replace( /<.*?>/g, '' )
Which fails for this due to the > in the attribute.
There is also the option of writing the content to a dummy element and then reading back the textContent, but that is horribly slow when dealing with a lot of data.
So, I was wondering if anyone has any suggestions? Removing the attributes - or at least the attribute values with the matching quotes in a first pass regex and then a second pass to strip the tags seems like it might be the way to go, but I can't quite get my head around it!
I believe this regex should work for your purpose:
/<([^\"\'>]*|\"([^"\\]|\\.)*\"|\'([^'\\]|\\.)*\')*>/g
Essentially, it matches any character other than ", ' or >, or it matches " or ' followed by any characters followed by another " or '.
Just use it in the same way as you did in the original question - string.replace(/<([^\"\'>]*|\"([^"\\]|\\.)*\"|\'([^'\\]|\\.)*\')*>/g, '');

How to add Unicode to HTML? [duplicate]

This question already has answers here:
How do I correctly insert unicode in an HTML title using JavaScript?
(2 answers)
Closed 7 years ago.
help me pls.
In Unicode symbol & = "&" + "#38;" If I add it to HTML like this
<div id="div" title="&"></div>
ALL OK! I create symbol in title, like this
div id="div" title="&"></div>
But, when I add Unicode in HTML via JavaScript:
var div = document.getElementById('div');
div.setAttribute('title', "&");
I have a bad result:
<div id="div" title="&"></div>
How I can add Unicode in html attribute via JavaScript and get the correct result like it:
<div id="div" title="&"></div>
Thanks for help!
AND
IF I do it:
var code = 26;
div.setAttribute('title', "\x" + code);
I have a error. How I can fix it?
HTML entity escaping (e.g. &) is, as the name implies, only necessary in HTML. It's not necessary in Javascript; you can set the character literally:
div.setAttribute("title", "&");
If you need to escape a character, you can do so using a hexadecimal character escape:
div.setAttribute("title", "\x26");
or a Unicode character escape:
div.setAttribute("title", "\u0026");
For the & character in Javascript, you do not have to escape it. Otherwise, you should can escape Unicode in Javascript with \u, but most of the time you will not have to.

Javascript / jQuery: How to convert string to HTML character code [duplicate]

This question already has answers here:
How to convert characters to HTML entities using plain JavaScript
(12 answers)
Closed 8 years ago.
I have a string that contains text including special characters like apostrophes, quotes, ampersand etc. that I would like to convert to HTML character codes, e.g. to show &#39 instead of an apostrophe etc. (as per the following list: http://www.w3.org/MarkUp/html-spec/html-spec_13.html.
I do not necessarily need to convert letters and spaces as long as the above and some other basic characters are encoded correctly.
For a temporary workaround I use simple replaces like str = str.replace("'", "&#39") but would like to avoid this as it only covers certain characters.
I was thinking of encodeURI or encodeURIcomponent but would like to know what is the best and easiest way to achieve this without encoding more than necessary.
Can someone tell what is the best way here if the idea is to avoid any issues with other JS where this is inserted dynamically, esp. thinking of single quotes, double quotes and any other character that could cause issues here (referring to JavaScript / jQuery and English language only) ?
function encodeEntities(value) {
return $('<div />').text(value).html();
}

Regex struct in Javascript [duplicate]

This question already has answers here:
How to convert unicode in JavaScript?
(4 answers)
Closed 9 years ago.
I am getting data returned in a JSON file with Unicode characters. How to replace Unicode characters as per my example?
\u003cli class=\"channels-content-item\"\u003e\n
\n
\u003cdiv class=\"shmoovie-content-cell\"\u003e\n
\u003ca href=\"\/movie\/the-makeover\" class=\"ux-thumb-wrap contains-addto yt-uix-sessionlink\" data-sessionlink=\"ei=Oo21UdLqM8aDhgHc_IHYCA\"\u003e
After replacing with regex:
\u003c must be replaced by <
\u003e must be replaced by >
\/ must be replaced by /
\" must be replaced by "
How to do that?
Using the bit of the string you posted I put this fiddle together that shows how to just use the string value you have (like in this SO answer I mentioned in the comments).
HTML
<div id="content"></div>
JS
var s = "\u003cli class=\"channels-content-item\"\u003e\n\n\u003cdiv class=\"shmoovie-content-cell\"\u003e\n\u003ca href=\"\/movie\/the-makeover\" class=\"ux-thumb-wrap contains-addto yt-uix-sessionlink\" data-sessionlink=\"ei=Oo21UdLqM8aDhgHc_IHYCA\"\u003e";
var div = document.getElementById('content');
div.innerHTML = s;
console.log(s);
Which sets the HTML content for the div with the elements:
<li class="channels-content-item">
<div class="shmoovie-content-cell">
<a href="/movie/the-makeover" class="ux-thumb-wrap contains-addto yt-uix-sessionlink" data-sessionlink="ei=Oo21UdLqM8aDhgHc_IHYCA">
Although it's not valid HTML, javascript seems to figure it out, at least it does in Chrome.
Not sure that regex is the best idea...The below solves the problem using the replace function built into JavaScript.
DEMO: http://jsfiddle.net/abc123/5DFUb/1/
var str = "\u003cli class=\"channels-content-item\"\u003e\n \n \u003cdiv class=\"shmoovie-content-cell\"\u003e\n \u003ca href=\"/movie/the-makeover\" class=\"ux-thumb-wrap contains-addto yt-uix-sessionlink\" data-sessionlink=\"ei=Oo21UdLqM8aDhgHc_IHYCA\"\u003e";
var newStr = str.replace("\\u003c", "<").replace("\\u003e",">").replace("\\/","/").replace("\\\"","\"");
alert(newStr);

Javascript replace doesn't work when it should [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 7 years ago.
var src = "http://blah.com/SOMETHING.jpg";
src.replace(/.*([A-Z])\.jpg$/g, "X");
at this point, shouldn't src be:
http://blah.com/SOMETHINX.jpg
If I use match() with the same regular expression, it says it matched. Regex Coach also shows a match on the character "G".
Try
src = src.replace(/.*([A-Z])\.jpg$/g, "X");
String#replace isn't a mutator method; it returns a new string with the modification.
EDIT: Separately, I don't think that regexp is exactly what you want. It says "any number of any character" followed by a captured group of one character A-Z followed by ".jpg" at the end of the string. src becomes simply "X".
The replace function doesn't change src.
I think what you want to do is:
src = src.replace(/.*([A-Z])\.jpg$/g, "X");
src.replace will replace the entire match "http://blah.com/SOMETHING.jpg", not just the part you captured with brackets.

Categories