0

I'm using js to read an xml file elements and replace some nodes name with another by replacing part of the string, but when running my app nothing happening, that's my code:

$('#dummy').load('cafe.xml',function() {
initialize();
})
function initialize(){
ct=$('menu').children().length;
for(cati==0;cati<=ct-1;cati++)
{
cats[cati]=$('menu').children().eq(cati).prop('nodeName');

// modifing the whitespaces and special characters
var realname = cats[cati];
if(realname.indexOf(".") != -1){
    realname.replace(/\./g,' ');
    }
if(realname.indexOf("1") != -1){
    realname.replace(/\1/g,'\'');
    }

if(realname.indexOf("2") != -1){
    realname.replace(/\2/g,'&');
    }

    if(realname.indexOf(":") != -1){
    realname.replace(/\:/g,'(');
    }

if(realname.indexOf("!") != -1){
    realname.replace(/\!/g,')');
    }

    if(realname.indexOf("0") != -1){
    realname.replace(/\0/g,'/');
    } 
}
2
  • 1
    You have more { than }. There must be a JS/parse error. Check it. Commented Dec 16, 2013 at 0:01
  • Don't forget to use var to declare local variables in your functions. Commented Dec 16, 2013 at 0:05

1 Answer 1

1

replace doesn't change the original string. Try with something like

realname = realname.replace(/.../g, "...");

Anyway, I'd ditch all those if, that are kind of useless given what you're doing.

Sign up to request clarification or add additional context in comments.

2 Comments

but what about the if thing ?
@MRefaat That's because you're checking if the string contains a character, but if it doesn't replace won't change anything anyway. If you remove the if statements, you can chain all those methods: realname = realname.replace(/\./g, " ").replace(/1/g, "'").replace(...)....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.