简体   繁体   中英

How to extract a value from a string using javascript

I have an variable which contains this value

<AirportCode>LKO</AirportCode> <CityOrAirportName>LUCKNOW AMAUSI</CityOrAirportName> <Country>India</Country> <CountryAbbrviation>IN</CountryAbbrviation> <CountryCode>733</CountryCode> <GMTOffset>-5.5</GMTOffset> <RunwayLengthFeet>7835</RunwayLengthFeet>

How can I extract the country code from this, in this case 733. Country code can be different, so I need to somehow get the value which is inside the CountryCode tag

You can create a div programmatically. Populate its innerHTML and then get CountryCode element.

 var data = "<AirportCode>LKO</AirportCode> <CityOrAirportName>LUCKNOW AMAUSI</CityOrAirportName> <Country>India</Country> <CountryAbbrviation>IN</CountryAbbrviation> <CountryCode>733</CountryCode> <GMTOffset>-5.5</GMTOffset> <RunwayLengthFeet>7835</RunwayLengthFeet>"; var div = document.createElement("div"); div.innerHTML = data; console.log(div.querySelector('CountryCode').textContent); 

Better parse XML using

 var data = "<AirportCode>LKO</AirportCode> <CityOrAirportName>LUCKNOW AMAUSI</CityOrAirportName> <Country>India</Country> <CountryAbbrviation>IN</CountryAbbrviation> <CountryCode>733</CountryCode> <GMTOffset>-5.5</GMTOffset> <RunwayLengthFeet>7835</RunwayLengthFeet>"; var oParser = new DOMParser(); var oDOM = oParser.parseFromString('<root>' + data + '</root>', "text/xml"); console.log(oDOM.querySelector('CountryCode').textContent) 

You can use XML parsing library, or just a simple RegExp if this is the only thing you want.

Just use the regex: /<CountryCode>(\\d+)<\\/CountryCode)/

For example:

var str = '<AirportCode>LKO</AirportCode> <CityOrAirportName>LUCKNOW AMAUSI</CityOrAirportName> <Country>India</Country> <CountryAbbrviation>IN</CountryAbbrviation> <CountryCode>733</CountryCode> <GMTOffset>-5.5</GMTOffset> <RunwayLengthFeet>7835</RunwayLengthFeet>';

function extractCountryCode(xmlStr) {
    var matches = xmlStr.match(/<CountryCode>(\d+)<\/CountryCode)/);
    if (matches.length == 2) {
        return matches[1];
    } else {
        return null;
    }
}

console.log(extractCountryCode(str));

You can use the functions indexOf and substring:

(Snippet added to show the result)

 var data = "<AirportCode>LKO</AirportCode> <CityOrAirportName>LUCKNOW AMAUSI</CityOrAirportName> <Country>India</Country> <CountryAbbrviation>IN</CountryAbbrviation> <CountryCode>733</CountryCode> <GMTOffset>-5.5</GMTOffset> <RunwayLengthFeet>7835</RunwayLengthFeet>"; var index1 = data.indexOf("<CountryCode>") + "<CountryCode>".length; var index2 = data.indexOf("</CountryCode>"); var code = data.substring(index1, index2); console.log(code); 

The addition of CountryCode.length is because the method indexOf will return the first occurrence of the text.

Hope this help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM