How to use this with JQuery - javascript

how do i use this code with jquery,
i know it's easy but it doesn't work for me.
document.getElementsByTagName('html')[0].innerHTML.replace(/<!--|-->/g,'')

$('html')[0].innerHTML.replace(/<!--|-->/g,'');

If your intention is to generate a new string where all <!-- and --> are removed then your code works just fine. If not then you probably should be reminded that in javascript, a String's replace() method does not replace anything in that string, it generates a new string. So:
var html = document.getElementsByTagName('html')[0];
html.innerHTML = html.innerHTML.replace(/<!--|-->/g,'');
But your question is very odd. Why would you want to do this? Perhaps you want to uncomment some commented out code? This does not look like something that is safe to do.

This should work for you. Remember you can use javascript and jQuery together. Try this:
$("<html>").html($("<html>").html().replace(/<!--|-->/g,''));
Probably not the most elegant solution, but should work. Are you familiar with:
http://visualjquery.com/

Related

Find and replace regex JavaScript

I know this is a super easy question, but I can't seem to wrap my head about it. I've got a bunch of URLs in varying languages such as:
www.myurl.com?lang=spa
www.myurl.com?lang=deu
www.myurl.com?lang=por
I need to create buttons to quickly switch from any language extension (spa, por, deu, rus, ukr, etc) to another language. I have the following code so far:
var url = window.location.toString();
window.location = url.replace(/lang=xxx/, 'lang=deu');
I just can't figure out the 3-character wildcard character. I know that I need to do some sort of regular expression or something, I'm just not sure how to go about it. Any help?
Thanks in advance
You can use
([&?]lang=)\w+
This will work with urls like www.myurl.com?foo=bar&lang=por&bar=foo too.
Instead of lang=deu, you'll have to replace with $1deu.
Try ... or .{3} or \w{3} or even [a-z]{3}, depending on how specific you want to be.
var s = 'www.myurl.com?lang=spa';
s.replace(/lang=[a-z]{3}/, 'lang=deu');
// => "www.myurl.com?lang=deu"
Here's a railroad diagram of the above example:
Use /lang=[a-z][3}/, here's an example:
/lang=[a-z]{3}/
Debuggex Demo

How to Split many Strings with Jquery

Let's say I have a bunch of strings which I get from user input. Each strings starts with a new line like the following example:
gordon:davis
harley:vona
empir:domen
furno:shyko
I would like to write a function in jQuery which loads the user data from the form and splits each row's sting. However I would ONLY need the first part of the strings, like:
gordon
harley
empir
furno
Is there any simple solution for this?
I have found something called: $.each(split) but I didn't get it work.
I'm sorry I'm a really newbie in Jquery, I hope you guys can help me out! Thanks in advance!
Use String.prototype.split method.
"gordon:davis".split(":") will return ["gordon","davis"]
Based on this, using Array.prototype.map or a JQuery version :
var strings = ["gordon:davis", "harley:vona"];
var splitStrings = strings.map(function (string) {
return string.split(":")[0];
});

javascript passing variables via url

Well, question maybe doesn't correct, but I give a shoot.
My script:
OMG, testing
In real, without javascript url look like:
http://localhost/url=google.com
In here wanna add this: &format=txt
So, the correct url then would be:
http://localhost/url=google.com&format=txt
How to add &format to the javascript "script" which showed at the
top?
is this what you want:
OMG, testing
Just add it to the link using +, unless I misunderstood the question.
OMG, testing
Assumes txt is a variable, otherwise put it all in the quotes.
This will do it ,, you have to do same as you did for the localhost
OMG, testing
like that :
OMG, testing
You only have to add the text to the string of your url

Remove tag but leave contents - jQuery/Javascript

I'm a bit tired tonight and just can't really think how to do this simply. Basically, I have something similar to the below and I want to strip out html tags but leave their contents:
some content and more and more content
and actually return the following:
some content and more and more content
Any help as always - massively appreciated!
EDIT: Thank you all so much for the answers - there I was going down the regular expression route, way to over complicate things! I actually ended up using .text() as suggested below, I've used this before but only to set, never to retrieve and it worked better as I was returning quite a large object! Thank you so much for all the suggestions :). I'll accept the answer after 6 minutes.
$('a').contents().unwrap()
Fiddle
$(selector).text() should strip out all the html.
This way is a little longer but maybe more self explanatory than the contents() method.
$('a').each(function () {
$(this).replaceWith($(this).html())
})
replaceWith accepts replacement html. (not selectors)
You can write
$(...).find('*')
.replaceWith(function() { return this.childNodes });
I don't know how well this will handle nested tags.
Perhaps something like this:
jQuery.fn.stripTags = function() {
return this.replaceWith( this.html().replace(/<\/?[^>]+>/gi, '') );
};
Source: http://www.mail-archive.com/jquery-en#googlegroups.com/msg18843.html

Javascript math parser library

Is there a good math parser in Javascript? I want to be able to parse something like:
LOG(3.14)+5^2+POW(2,LN(X*2,Y))
Thanks,
Here is a brand new initiative:
http://mathjs.org
Comes with an extensive and easy to use parser which also supports assignment and usage of variables and functions like in your example expression.
Use this one. It defined an "operator" object that lets you define your own operators.
http://jsfromhell.com/classes/math-processor
Warning: it uses with. If you don't know why that's dangerous, find out before using this code in anything critical. Alternately, you could just re-write it without with.
Try nerdamer
var result = nerdamer('log(3.14)+5^2+2^(log(X*2)/log(Y))').evaluate();
document.getElementById('text').innerHTML = result.text();
<script src="http://nerdamer.com/js/nerdamer.core.js"></script>
<div id="text"></div>
I know it's an old question, but I found it by chance and I have something to help. Not as complete as mathjs, but useful and fast.

Categories

Resources