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?
1 Answer 1
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
Sign up to request clarification or add additional context in comments.
Comments
lang-js
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.