Why does the Javascript replace() function not do anything? [closed] - javascript

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
Here's my function:
CSS_Left = '450px';
CSS_Left.replace('px', '');
alert(CSS_Left);
It returns '450px'
Here's an example:
http://jsfiddle.net/D2Mgb/
I want it to replace 'px' with nothing.

In JavaScript, Strings are immutable.
All types except objects define immutable values. Specifically, strings are immutable (unlike in C for instance).
So, str.replace returns a new string. Try this instead:
CSS_Left = CSS_Left.replace('px', '');
Or if you don't want to overwrite the original string, just do it in the alert call
alert(CSS_Left.replace('px', ''));
It's worth noting that .replace can also take a regexp. Though it's not necessary in your case, you could do something like this to achieve the same result
CSS_Left = CSS_left.replace(/px$/, '');

Because .replace() return a new string. If you want to update the old string you should write:
CSS_Left = CSS_Left.replace('px', '');

I am not sure, but the function replace() creates a new variable.
Try this :
var CSS_Left = '450px';
CSS_Left = CSS_Left.replace('px', '');
alert(CSS_Left);

The output of the replace call needs to be reassigned to CSS_Left

Related

? Incorrect regex expression [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I’ve been trying to do find if a variable contains a digit inside of it. And for some reason, I put (exp.) ‘Majorpassword09’ and it just prints a false?
// vars
var regex = new RegExp(‘.*\d.*’)
var str = ‘Scorpio08’
// main
function reg1() {
if (regex.test(str)){
console.log(‘true’);
}else{
console.log(‘false’);
}
}
reg1();
EDIT: I’ve put the title as Incorrect regex expression because that's the only thing that I think is wrong.
You're most likely on a mac.
Your code shows that you're using fancy quotes. Like these ones: ‘’. You need to use normal quotes, like ', and ".
Here's a solution to your problem.
// vars
var regex = new RegExp(".*\\d.*")
var str = "Scorpio08"
// main
function reg1() {
if (regex.test(str))
{
console.log("true");
}else{
console.log("false");
}
}
reg1();
Whatever text editor you're using, it has fancy quotes on. Consider looking in the settings, or on Google, to find out how to disable them.
as you are using string to create a RegExp Object thus have to use two backslashes to properly escape
var regex = new RegExp(‘.*\\d.*’)
or
var regex = /.*\d.*/

Iterating through all the instances with name having a common string [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 8 years ago.
Improve this question
Is there a way to access all the object instances starting with a common string.
Example: I have instances named button64, button223, button856471, button229846, etc. I have no control over how these instances are named. I want to push all these in an array.
I am not creating these objects, I just have to write a javascript which sits in this HTML page and collects all the object instances starting with the string 'button'.
The purpose is to reach out to a desired object out of these and change the visual element.
Also, the solution has to be compatible with IE8. Any ideas about how should I iterate?
If you can use jQuery, you could write:
var buttonsArray = [];
$("div").each(function() {
var id = $(this).attr("id");
if (id && id.indexOf("button") == 0) {
buttonsArray.push($(this));
}
});
You can use regular expressions to find any expression matching the pattern. Using the match method, every instance of the pattern is returned as an array.
var str = document.getElementById("sample");
var arr = string.match(/button[\d]*/g);
The regular expression on line two will match any result that has "button" and will stop once it encounters a character afterwards that is not a digit.

jQuery: how to get URL and modify it? [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
I have a link:
http://blog.isvn.pl/search/music
How can I modify this link with jQuery to get:
http://blog.isvn.pl/tagged/music
I want change only „search“ to „tagged“, and open new link.
You can use replace()
var url = 'http://blog.isvn.pl/search/music';
var newUrl = url.replace('/search/', '/tagged/');
window.location.href = newUrl;
//or, to open in a new window:
window.open(url,'_blank');
Demo
You can use
var value = 'http://blog.isvn.pl/search/music';
value = value.replace("search", "tagged");
alert(value);
use the replace function in javascript:
var currentUrl = "http://blog.isvn.pl/search/music";
var replacedUrl = currentUrl.replace("/search/", "/tagged/");
I use /search/ (with the slashes) so it only replaces when it is a folder in the path. Otherwise it would also replace when the word search in in a song title.
You can use replace(), to replace search with tagged
Live Demo
url = url.replace('search', 'tagged');
To change the url
window.location.href = window.location.href.replace('search', 'tagged');
OR
window.location.href = document.URL.replace('search', 'tagged');

get id by getElementById then set value [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
I wonder why my code doesn't work, btw I was learning javascript object, so ignore the rest of lines of code.
http://jsfiddle.net/aYK8K/
var p = document.getElementById(p)
p.innerHTML("Hmm Hmm Good");
According to your fiddle you have
<p id="p"></p>
So you will want "p" not p
var p = document.getElementById("p"); // note use of string
p.innerHTML = "Hmm Hmm Good";
To make this less confusing, maybe you should have something like
<p id="hello"></p>
Then
var hello = document.getElementById("hello");
hello.innerHTML = "hello!";
This way you avoid brain dizziness by using different names for your elements, IDs, and JavaScript variables.
its p.innerHTML = "Hmm Hmm Good";
you should change p = document.getElementById(p) to p = document.getElementById('p')
I think you have a confusion with jquery syntax,using jquery you can do like
$('p').html('Hmm Hmm Good');
There's quite a few things wrong with your solution:
For starters, in your object you need to do the following:
var p = document.getElementById('p')
p.innerHTML = 'Hmm Hmm Good';
Next, outside your object you actually need to call the method otherwise nothing will work:
mango.howSweetAmI();
Fiddle: http://jsfiddle.net/KyleMuir/aYK8K/2/
getElementById expects a string
var p = document.getElementById('p')
innerHTML is not a function
p.innerHTML = "Hmm Hmm Good"
good luck!

Get the value of a div [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
Helllo, I'm having a div like this:
<div>1+1</div>
How can I get these '1+1' to the javascript and calculate them to '2'?
Thanks in advance!
you can use innerHTML property of element by javascript to extract the value:
document.getElementById("div").innerHTML
and then use eval() method to evaluate the math expression:
var exp=document.getElementById("div").innerHTML;
alert(eval(exp));
You need to somehow identify this div, e.g
<div id="div-with-expression">1+1</div>
And code will be like:
console.log(eval(document.getElementById('div-with-expression').innerText));
You can use eval:
var equation = getElementById(IdOfDiv).innerHTML;
var result = eval(equation);
Well, You could use the Javascript eval() func:
http://www.w3schools.com/jsref/jsref_eval.asp
But, eval functions can introduce malicious code into your environment.
I'm not sure as to what your purpose to start with and what your assumptions are for that input but here's another way to go about it with regex although it's more complicated:
var myRegexp = /(\d+)(\+)(\d+)/;
var match = myRegexp.exec("1+1");
// parseInt(match[0]) == 1 ,as an Int
// match[1] = '+', you could figure what operand this is with 'if else' conditions
// parseInt(match[2]) == 1 ,as an Int

Categories

Resources