Decode HTML Entities in Javascript [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a record in the database like: <script src="here is link"></script>.
It's regexp for convert slash to special HTML char: string.replace(/\<\/script\>/g, '</script>');
Then I output it to the page.
It's result after output:
<script src="http://code.jquery.com/jquery-latest.min.js">
</script>
</script>
Why outputs HTML char? I need convert it again with regexp?

Use these JavaScript functions:
function decodeHTMLEntities(str) {
return str.replace(/&#(\d+);/g, function(match, dec) {
return String.fromCharCode(dec);
});
};
var encodeHtmlEntity = function(str) {
var buf = [];
for (var i=str.length-1;i>=0;i--) {
buf.unshift(['&#', str[i].charCodeAt(), ';'].join(''));
}
return buf.join('');
};
alert(decodeHTMLEntities('</script>'))
DEMO http://jsfiddle.net/tuga/6pXmn/3/
SRC https://gist.github.com/CatTail/4174511

Related

javascript from string to array. split my regex [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I need give from text [{"value":"opa"},{"value":"opa2"},{"value":"opa3"}]
only opa...
and set it to array like var array = date.split(regex);
that array[0]="opa" and array[1]="opa2".
Please help me.
var a='[{"value":"opa"},{"value":"opa2"},{"value":"opa3"}]';
var b=JSON.parse(a);
c = [];
b.forEach(function(entry) {
c.push(entry.value);
});
alert(c[0]);
You have array of objets, the task is filter and turn this objets into strings.
var array = [{"value":"opa"},{"value":"opa2"},{"value":"o1pa3"}];
array = array.map(function(item){
if(/opa/.test(item.value)){
return item.value;
}
else return null;
}).filter(function(item){ return item; });
console.log(array)
If you want to parse string and convert to JSON object just use
var array = JSON.parse(STRING);
You can play with this demo

How to split each word from a string and store it in different variables [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
So here's the case, I have a variable with a string
var text = "This is a string";
Is it possible to take every word, except for the spaces, " ", of this string and put it into a seperate variable? Like for example:
var 1 = "This";
var 2 = "is";
var 3 = "a";
var 4 = "string";
Thanks in advance.
var text = "This is a string";
var splitted = text.split(" ");
console.log(splitted);
Output
[ 'This', 'is', 'a', 'string' ]
You can access the individual elements like this
console.log(splitted[1]); // will print is

Javascript String return inside script tag [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I'm not so good with regex or trying to return the side part of a string. Can someone help me figure this out. I have a demo below.
str = "<html><head><script>var x = '123';</script></head></html>";
console.log(str)
// should return var x = '123';
Someone wrote a very good regex for stripping tags:
var strippedStr = str.replace(/(<([^>]+)>)/ig,"");
console.log(strippedStr);
Source: http://css-tricks.com/snippets/javascript/strip-html-tags-in-javascript/
I played around a bit with it and found a way with match using groups:
str.match(/(>)([^><]+)(<\/)/m)[2]
result = "var x = '123';"
=> a range (2nd group) beginning by ">" (1st group) and ending with "var x = '123';
I am not sure it'll cover all the cases...

getting all true keys from object using angular and/or underscore [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have an object in Angular looking like this:
$scope.addEmployeeDepartments={ 5066549580791808=true, 6192449487634432=true, 7192449487634432=false}
How do I generate a comma-separated string like this
var ids="5066549580791808, 6192449487634432"
containing all the keys which are true ?
Btw, I am using underscore.js in other parts of my solution, so I don't know if that makes it easier.
thanks
Thomas
You can do this with reduce in one pass:
var collect_trues_in = function(a, v, k) {
if(v)
a.push(k);
return a;
};
var csv = _($scope.addEmployeeDepartments).reduce(collect_trues_in, [ ]).join(', ');
Demo: http://jsfiddle.net/ambiguous/6CEZW/

Explanation of Regular Expression [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can anyone explain what is meaning of this Regular Expression?
/^(https?):\/\/(www\.)?[a-zA-Z0-9\-\.]+\.[a-zA-Z0-9]{2,3}[a-zA-Z0-9\-\#\.\/\?]*$/
Finally I have founded my solution:
function validateURL(url)
{
var re = /^(https?):\/\/(www\.)?[a-z0-9\-\.]+\.[[a-z0-9\-\.\/]{2,}]*$/;
if (!re.test(url))
{
return false;
}
else
{
return true;
}
}
this function return true if your url contain these:
part 1. https or http
part 2. www or not (means optional).
Below image exploring in more depth.
I am wrong?
I've tried the following and works fine for me.
var url = "https://www.xyz.xe";
function validateURL(url)
{
var urlregex = new RegExp("^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*#)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
return urlregex.test(url);
}
validateURL(url);
Returs false when url="https://www.xyz.x" and true when url="https://www.xyz.xe"

Categories