0

This is my code

var tmp = document.body.outerHTML.match(/<b>Location: <\/b>([^,]+),([^,]+),([^,])([^\s]+) <br>/i);
DataExtractor.AddHeader(14, 'id_country');
 var country
 if( tmp ){country = tmp[1]
 }
 else{alert('country is not set')} 
DataExtractor.AddResult(14, country);`

This returns the value of 'UK' or 'USA' or whatever is the country code is, but I need to replace this value to correspond with a country ID stored elsewhere so they can be compared. I therefore need to be able to tell this piece of code to replace the value UK with 75. I have tried the following, but it just didn't work.

var tmp = document.body.outerHTML.match(/<b>Location: <\/b>([^,]+),([^,]+),([^,])([^\s]+) <br>/i);
DataExtractor.AddHeader(14, 'id_country');
 var country
 if( tmp ){country = tmp[1]
 if (country){
 for (var i = 0; i < country.length; i++) {
 country[i] = country[i].replace("UK","75")
}
 else{alert('country is not set')}
DataExtractor.AddResult(14, country[i]);
}}

Does anyone see where it's gone wrong?

asked May 22, 2013 at 13:40
2
  • 2
    The code you have posted here is not syntactically valid -- where should the closing quote and parenthesis go in that final alert? Additionally, it would be helpful if you could indent your code blocks so it's easier for people to understand your problem and answer your question. Commented May 22, 2013 at 13:42
  • Sorry have amended the errors as mentioned Commented May 22, 2013 at 14:17

1 Answer 1

3

country contains a string - eg "UK" and you are then taking each character of it "U" or "K" and trying to replace "UK" with "75" which never matches.

if (country){
 country = country.replace("UK","75")
}

This should work

answered May 22, 2013 at 14:26
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.