Finding values in strings

indexOf(), match() both provide the functionality to test whether or not a text string or array (e.g. lookup, list of descendants) contains a specified value

indexOf

Use indexOf() to find the position in which a value appears in a list

For example, ['a','b','c'].indexOf("c") would return 2 (remember that in orgvue, the index of the first item in a list is 0)

Note: indexOf() must be entered into expression with the "O" capitalised

Example:

string(node.PositionTitle).indexOf ("Manager")

Returns the numerical index at which the word "Manager" appears in the Position Title value "HR Manager"

The use of String() at the start of the expression ensures the value being tested by the expression is done as a string

Note String() must be entered with capital S

image

The results are case sensitive, so transforming the property value to upper/lower case using toUpperCase() or toLowerCase() is a good way of avoiding incorrect results because of inconsistencies in case

String(node.PositionTitle).toUpperCase().indexOf("MANAGER")

image

The indexOf() method requires an unwrapped value, so you need to type String(node.propertyName) if you are referencing a property in orgvue

If a value doesn't appear in the specified list, indexOf() returns a result of -1, meaning it can conveniently be used as a "contains" type function,

e.g.:

["A", "B", "C"].indexOf(String(node.grade)) > -1
    ? "Senior Manager"
    : "Other"

If node.grade is one of the 3 grades in the array, return "Senior Manager", else return "Other"

image

match

Use match() if you want to search for a more specific value

This is because it can be used in conjunction with Regular Expressions (regex) which are a very powerful and commonly used form of syntax for searching for patterns of characters, for example in search engines

For more information about the match() syntax, see the summary of Regular Expressions (Regex) Webpage or test them out yourself at http://regexr.com/

match() returns the string you are searching for if it is found, otherwise it returns a blank

Like indexOf(), match() is also case sensitive and requires a String(node.propertyName) rather than just node.propertyName

An alternative to matching whole strings and the complexity of case sensitivity is to use Regular Expressions

String matching

Using match() it is possible to check if a string appears in a property

String(node.PositionTitle).toUpperCase()
    .match("HEAD")
  • A. Returns the word "HEAD" if it appears in the Position Title value
  • B. Returns null if no match is found

image

Matching of characters

It is also possible look for specific characters in a text string using regular expressions (Regex)

String(node.costcentre).match(/[0-9]/)
  • A. Matches the first number found within the value for Cost Centre

Adding the g global flag to the expression

String(node.costcentre).match(/[0-9]/g)
  • B. Matches all numbers found within the value for Cost Centre as separate results

If the format is known then the expression can be enhanced by adding the number of characters to be matched within {4}

node.costcentre.value.match(/[0-9]{4}/g)
  • C. This matches the 4 numbers as a single array in results

image

Sophisticated Pattern matching

match() can be used to look for more sophisticated pattens using these Regex expressions

An example would be to check a National Insurance number confirms to a specific format

In this case two letters followed by six numbers followed by a letter

String(node.niOrTaxNumber).match(/[A-Za-z]{2}[0-9]{6}[A-Za-z]/g)

image

This Regex expression could be extended even further if the letters and numbers are grouped in pairs with spaces between them

String(node.niOrTaxNumber).match(
  /[A-Za-z]{2}\s[0-9]{2}\s[0-9]{2}\s[0-9]{2}\s[A-Za-z]/g)

image

results matching ""

    No results matching ""

    results matching ""

      No results matching ""