Upland OL User community

Check if string contains multiple substrings

Hi, I would like to be able to check if an extraction contains specific words. Currently I have an if statement searching the string for various substrings, but this does not search for the exact word so could find the string within another word. My current if statement is below:

var result;
var field = record.fields.Address_SubField1;
if (field.includes(“January”)) {result="";}
else if (field.includes(“February”)) {result="";}
else if (field.includes(“March”)) {result="";}
else if (field.includes(“April”)) {result="";}
else if (field.includes(“May”)) {result="";}
else if (field.includes(“June”)) {result="";}
else if (field.includes(“July”)) {result="";}
else if (field.includes(“August”)) {result="";}
else if (field.includes(“September”)) {result="";}
else if (field.includes(“October”)) {result="";}
else if (field.includes(“November”)) {result="";}
else if (field.includes(“December”)) {result="";}
else if (field.includes(“Dear”)) {result="";}
else {result=field;}
result;

I sure this is possible but I cannot work out how. Any help would be greatly appreciated.
Thanks

Regular expressions to the rescue!!! :slight_smile:

Use something like this:

var field = record.fields.Address_SubField1;
var match = field.match(/\b(?:january|february|march|april|may|june|july|september|october|november|december)\b/i)
var value = (match ? match[0] : "not found").toLowerCase();
var result="";
switch(value){
	case "january":
	case "february":
	case "march":
		result="winter";
		break;
	case "april":
	case "may":
	case "june":
		result="spring"
		break;
	case "july":
	case "august":
	case "september":
		result="summer"
		break;
	case "october":
	case "november":
	case "december":
		result="fall"
		break;
	default:
		result="undefined";		
}
result;

This code checks for month names as whole words and if it finds one, it gets stored in the value variable. After that, you can process it any way you want (I’m assuming you don’t want to determine the actual season of the year as the code above does…:stuck_out_tongue: )

Many Thanks Phil much appreciated.