admin管理员组文章数量:1405308
I have somewhat of an odd situation, where I need to fix a bug in a website where, when a string is created (dynamically) it adds 5 spaces before the string and 5 spaces after the string. Obviously, the best thing to do would be to fix the back end code and get rid of those spaces... long story short, I can't and I have to do it with javascript. I'm not quite sure how to do it, but this is what I was thinking
<!--Dynamically generated string including spaces added in backend-->
<span id="balance"> 245.34 </span>
My idea was to do the following with javascript
function removespace()
{
var oldString = document.getElementById('balance');
var newString = (THIS IS WHERE I AM STUCK... I NEED TO REMOVE THE SPACES);
document.getElementByID('balance').innerHTML = newString;
}
Does anyone have any suggestions? Thanks!
ALSO FORGOT TO MENTION: I can't use any javascript libraries like prototype or jquery.
Edit: I have this so far... but it doesn't seem to be working:
<span id="balance"> $245.00 </span>
<script>
function removespace()
{
var oldString = document.getElementById('balance');
var newString = oldString.trim ();
document.getElementByID('balance').innerHTML = newString;
}
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");
}
</script>
here is the solution I used... I finished it before I saw the other updates... but everyone was very helpful
function trim(stringToTrim) {
return stringToTrim.replace(/^\s+|\s+$/g,"");
}
var oldString = document.getElementById('balance').innerHTML;
var newString = trim(oldString);
document.getElementById('balance').innerHTML = newString;
I have somewhat of an odd situation, where I need to fix a bug in a website where, when a string is created (dynamically) it adds 5 spaces before the string and 5 spaces after the string. Obviously, the best thing to do would be to fix the back end code and get rid of those spaces... long story short, I can't and I have to do it with javascript. I'm not quite sure how to do it, but this is what I was thinking
<!--Dynamically generated string including spaces added in backend-->
<span id="balance"> 245.34 </span>
My idea was to do the following with javascript
function removespace()
{
var oldString = document.getElementById('balance');
var newString = (THIS IS WHERE I AM STUCK... I NEED TO REMOVE THE SPACES);
document.getElementByID('balance').innerHTML = newString;
}
Does anyone have any suggestions? Thanks!
ALSO FORGOT TO MENTION: I can't use any javascript libraries like prototype or jquery.
Edit: I have this so far... but it doesn't seem to be working:
<span id="balance"> $245.00 </span>
<script>
function removespace()
{
var oldString = document.getElementById('balance');
var newString = oldString.trim ();
document.getElementByID('balance').innerHTML = newString;
}
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");
}
</script>
here is the solution I used... I finished it before I saw the other updates... but everyone was very helpful
function trim(stringToTrim) {
return stringToTrim.replace(/^\s+|\s+$/g,"");
}
var oldString = document.getElementById('balance').innerHTML;
var newString = trim(oldString);
document.getElementById('balance').innerHTML = newString;
Share
Improve this question
edited Nov 24, 2010 at 14:49
Bill
asked Nov 24, 2010 at 13:58
BillBill
5,68817 gold badges66 silver badges97 bronze badges
3
- Why do you have to remove the spaces? HTML press continuous spaces to one. So the rendered HTML page should only show one space before and after the value (but ok, this might still be undesirable). – Felix Kling Commented Nov 24, 2010 at 14:04
- Because even if it shows 1 space before or after it breaks the design of the page – Bill Commented Nov 24, 2010 at 14:06
- Ok. I assumed it but I wanted to verify... sometimes people make a problem although they don't have any ;) – Felix Kling Commented Nov 24, 2010 at 14:13
6 Answers
Reset to default 7Unfortunetly JavaScript does not have a trim()
function. But you can roll your own:
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");
}
and then do:
var newString = oldString.trim ();
the above function is from this website (first result on google for "javascript trim")
edit (based on your update to your question and ments):
change
var oldString = document.getElementById('balance');
to
var oldString = document.getElementById('balance').innerHTML;
and change
document.getElementByID('balance').innerHTML = newString;
to
document.getElementById('balance').innerHTML = newString; // notice the lower case d
and you have to call the removespace
function at some point (but I'm sure you already do that) :)
Writting out of my head here.
var elem = doc.getElementById('balance');
var oldString = elem.innerHTML;
elem.innerHTML=oldString.(/^\s+|\s+$/g,"")
Something like that?
Edit: yep, only my first space is deleted, so I stole the whitespace from the answer number 1 :)
You try something like this
str.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, "");
use trim() to remove all white spaces
var newString = oldString.trim();
or use replace to replace white spaces with empty string:
var newString = oldString.replace(" ","");
with jQuery it's simple:
var newStr = jQuery.trim(oldStr);
If balance is meant to be a double you could convert it to a string:
var c = '1234';
d = c * 1;
then back to a string if need be by:
d = c.toString();
本文标签: htmlremove parts of string with javascriptStack Overflow
版权声明:本文标题:html - remove parts of string with javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744294292a2599267.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论