My script receive a string from the user that contains city name or more than one, I want to extract the city name. My current code split the string to words based on white space but if the city name is two words that will not work.
I can have an array of expected city names and I want to match the city name of the string against that array, is there any possible way without having to use neural language processing techniques ??
Example
var cities = ['Cairo', 'Dubai', 'San Francisco']
// possible strings
var user_string = 'traveling to Cairo' // result ['Cairo']
var user_string = 'will leave San Francisco to Dubai late' // result ['San Francisco', 'Dubai']
Create a RegExp and use match
user_string.match(new RegExp( cities.join("|"), "gi" ) )
Demo
var fnMatches = ( cities, str ) => str.match(new RegExp( cities.join("|"), "gi" ) );
var cities = ['Cairo', 'Dubai', 'San Francisco'];
console.log( fnMatches( cities, "traveling to Cairo" ) );
console.log( fnMatches( cities, "will leave San Francisco to Dubai late" ) );
You should use regular expressions to match the cities
for(i=0; i < cities.length; i++) {
if(user_string.match(new RegExp(cities[i], 'i'))){
console.log(cities[i]);
}
}
How can I capture a word just after specific word in regex, I have to select everything between from - to and after to so there will be two capturing groups.
Example:
"From London to Saint Petersburg" I wanted to extract London Saint Petersburg from above string.
Im stuck with this code here, my current regex selecting to Saint Petersburg i wanted to get rid word from and to from the selection.
/(?=to)(.*)/i
You can capture the two groups you need and then use match to extract them:
s = "From London to Saint Petersburg"
console.log(
s.match(/From (.*?) to (.*)/).slice(1,3)
)
you can just use split() and use /From | to /, it will return an array containing split values
var str = "From London to Saint Petersburg";
var arr = str.split(/from | to /ig);
console.log(arr);
Here is sample code doing what you asks for:
<html>
<head>
</head>
<body>
</body>
<script>
var strIn = "From London to Saint Petersburg";
var regEx = /^From\s(.+?)\sto\s(.+?)$/;
var arrResult = regEx.exec(strIn);
var strOut = "Original:" + strIn + "<br>Result:<br>";
strOut += "1. " + arrResult[1] + "<br>";
strOut += "2. " + arrResult[2];
document.write(strOut);
</script>
</html>
Place this in a document. Open it with a browser. Here is how the result looks like:
Original:From London to Saint Petersburg
Result:
1. London
2. Saint Petersburg
Hope it helps!
If i have a string say, 1234 Newyork Street, America and i want to extract the first LETTER from the string.
I understand how to use
string.charAt(0);
But this extracts for example '1' from the example above. How would i modify the code so if i enter
string.charAt(0);
I extract the first LETTER which is 'N'.
string.replace(/[^a-zA-Z]/g, '').charAt(0);
This will remove anything that is not a letter, then return the first letter.
Use search to get the index of the first letter, then use charAt:
var s = "1234 Newyork Street, America";
s.charAt(s.search(/[a-zA-Z]/));
Both of these will work :
string.replace(/^[1-9\s]+/g,"")[0]
or
replace(/^[1-9\s]+/g,"").charAt(0)
You can use regular expression capturing to find the first letter:
var s = "1234 Newyork Street, America",
result = s.match(/([a-zA-Z]).*/),
firstLetter;
if(result) {
firstLetter = result[1];
}
I have the following array of data named cityList:
var cityList = [
"Anaa, French Polynesia (AAA)",
"Arrabury, Australia (AAB)",
"Al Arish, Egypt (AAC)",
"Ad-Dabbah, Sudan (AAD)",
"Annaba, Algeria (AAE)",
"Apalachicola, United States (AAF)",
"Arapoti, Brazil (AAG)",
"Aachen, Germany (AAH)",
"Arraias, Brazil (AAI)",
"Awaradam, Suriname (AAJ)",
"Aranuka, Kiribati (AAK)",
"Aalborg, Denmark (AAL)"
];
I want to first search the city name starting at the beginning of the string.
Next I want to search the code portion of the string: AAA, AAB, AAC, etc...
I want to apply a search pattern as a javascript regular expression, first to the city name, and second to the city code.
Here are my regular expressions:
// this regular expression used for search city name
var matcher = new RegExp("^" + re, "i");
// this regular expression used for search city code
var matcher = new RegExp("([(*)])" + re, "i");
How do I combine these two regular expressions into a single regex that works as described?
I suggest this:
var myregexp = /^([^,]+),[^(]*\(([^()]+)\)/;
var match = myregexp.exec(subject);
if (match != null) {
city = match[1];
code = match[2];
}
Explanation:
^ # Start of string
( # Match and capture (group number 1):
[^,]+ # One or more characters except comma (alternatively insert city name)
) # End of group 1
, # Match a comma
[^(]* # Match any number of characters except an opening parenthesis
\( # Match an opening parenthesis
( # Match and capture (group number 2):
[^()]+ # One or more characters except parentheses (alt. insert city code)
) # End of group 2
\) # Match a closing parenthesis
This assumes that no city name will ever contain a comma (otherwise this regex would only capture the part before the comma), so you'd need to check your data if that's ever possible. I can't think of an example, but that's not saying anything :)
$("#leavingCity").autocomplete({
source: function(req, responseFn) {
var re = $.ui.autocomplete.escapeRegex(req.term);
var matcher = new RegExp("/^([^,]+),[^(]*\(([^()]+)\)/", "g");
var a = $.grep(cityList, function(item,index) { return matcher.test(item); });
responseFn(a);
} });
Try this, regualr expression by Tim Pietzcker
This is the most elegant way I can do it:
var cityList = ["Anaa, French Polynesia (AAA)","Arrabury, Australia (AAB)","Al Arish, Egypt (AAC)","Ad-Dabbah, Sudan (AAD)","Annaba, Algeria (AAE)","Apalachicola, United States (AAF)","Arapoti, Brazil (AAG)","Aachen, Germany (AAH)","Arraias, Brazil (AAI)","Awaradam, Suriname (AAJ)","Aranuka, Kiribati (AAK)","Aalborg, Denmark (AAL)"];
var regex = /([a-z].+?),.+?\(([A-Z]{3,3})\)/gi, match, newList = [];
while (match = regex.exec(cityList)) {
newList.push(match[1]+" - "+match[2]);
}
alert(newList[7]);
// prints Aachen - AAH
If you don't understand how to use parentheses in your regex, I suggest you check out the site I learned from: http://www.regular-expressions.info/
Here I suggest a completly different approach (ECMA-262 standard).
As using the regex requires a linear search anyway, if you can pre-process the data, you can set up an array of city objects:
function City(name, country, code){
this.cityName = name;
this.cityCountry = country;
this.cityCode = code;
}
var cities = [];
cities.push(new City('Anaa', 'French Polynesia', 'AAA'));
// ... push the other cities
And a search function:
function GetCity(cityToSearch, cities){
var res = null;
for(i=0;i<cities.length;i++){
if(cities[i].city = cityToSearch
res = cities[i];
}
return res;
}
At run time:
var codeFound = '';
var cityFound = GetCity('Arraias');
if(cityFound != null)
codeFound = cityFound.cityCode;
Remark
In both case, if you are going to fill the cities array with all city of the world, the city name is not a key! For instance there are half a dozen of 'Springfield' in USA. In that case a better approach is to use a two-fields key.
I think you want to accomplish this in a few simple steps:
Split each string in your array before and after the first parenthesis
Apply your first regex to the first part of the string. Store the result as a boolean variable, perhaps named matchOne
Apply your second regex to the second part of the string (don't forget to remove the closing parenthesis). Store the result as a boolean variable, perhaps named matchTwo.
Test if either of the two mathes succeeded: return ( matchOne || matchTwo );
Use indexOf
Its more efficient and explicit of expectation. regex is unnecessary.
const isMatchX = cityList.indexOf('AAB');
const isMatchY = cityList.indexOf('Awar');
Alternatively you could so something like this but its way overkill when you can use indexOf:
const search = (cityList, re) => {
const strRegPart1 = "¬[^¬]*" + re + "[^¬]*";
const strRegPart2 = "¬[^¬]*\\([^\\)]*" + re + "[^\\)]*\\)($|¬)";
const regSearch = RegExp("(" + strRegPart1 + "|" + strRegPart2 + ")", "gi");
const strCityListMarked = '¬' + cityList.join('¬');
const arrMatch = strCityListMarked.match(regSearch);
return arrMatch && arrMatch[1].substr(1);
}
I have a string like this:
Franciscan St. Francis Health - Indianapolis
I need to extract everything after '-' including the dash itself and output it in the second line..How do I extract everything before '-'?? Regex or jquery?
The string infront of '-' will be dynamic and could have varying number of letters...
Neither. I would just use the native .split() function for strings:
var myString = 'Franciscan St. Francis Health - Indianapolis';
var stringArray = myString.split('-');
//at this point stringArray equals: ['Franciscan St. Francis Health ', ' Indianapolis']
Once you've crated the stringArray variable, you can output the original string's pieces in whatever way you want, for example:
alert('-' + stringArray[1]); //alerts "- Indianapolis"
Edit
To address a commenter's follow-up question: "What if the string after the hyphen has another hyphen in it"?
In that case, you could do something like this:
var myString = 'Franciscan St. Francis Health - Indianapolis - IN';
var stringArray = myString.split('-');
//at this point stringArray equals: ['Franciscan St. Francis Health ', ' Indianapolis ', ' IN']
alert('-' + stringArray.slice(1).join('-')); //alerts "- Indianapolis - IN"
Both .slice() and .join() are native Array methods in JS, and join() is the opposite of the .split() method used earlier.
Regex or jquery?
False dichotomy. Use String.splitMDN
var tokens = 'Franciscan St. Francis Health - Indianapolis'.split('-');
var s = tokens.slice(1).join('-'); // account for '-'s in city name
alert('-' + s);
DEMO
join()MDN
slice()MDN
Probably no need for regex or jquery. This should do it:
var arr = 'Franciscan St. Francis Health - Wilkes-Barre'.split('-');
var firstLine = arr[0]
var secondLine = arr.slice(1).join('-');
Ideally, your data would be stored in two separate fields, so you don't have to worry about splitting strings for display.