Provides a string manipulation node with a chainable UI based on the concise and lightweight stringjs.com.
Applies string methods from a given context property and assigns the results optionally to the same or another context property. Methods can be chained by adding multiple methods sequentially and with parameters that are literal or from other context properties. The string object always passes along the msg object with (if any) changes applied.
By default, string "from" and "to" apply to the
msg.payload property but may
be set to any property of
Data Extraction - Get Technical FAX
Methods can be chained within the UI enabling complex and versatile parsing functionality. Here we can see how easy it is to extract the technical FAX phone number WhoIS information:
Consider the given WhoIS dump from a command line console (see image below). We will want to obtain the FAX number (outlined in the dashed yellow). The string node will extract the technical FAX phone number by first removing the header, followed by all data up to the phrase "Technical:". This ensures that we don't accidentally obtain a FAX number from another section. Lastly, the string node grabs the number from between the markers "Fax:" and a carriage return and displays the output in the Node-RED debug window.
console.log( msg.payload.getRightMost('%') .delLeftMost('Technical:') .between('FAX:', '\n') );
Validate Phone Number
Furthermore, a single string node could have the properties set to perform data validation. In this simple example we'll perform some conditional checks to see if the phone number is numeric and furnish a friendly error message if it is not. Consider the following flow:
Let's take a look at the string node titled "Verify Number" (see image below).
The properties for the node have been configured to use the methods
isNumeric which will result in a boolean true or false.
Next, we convert the boolean to a string using
toString. Lastly, we use the
replaceAll methods to convert the only two boolean possibilities to the
original number as found in property msg.payload or the informative error message
"Error! Not a valid number.".
Run the following command in your Node-RED user directory (typically ~/.node-red):
npm install node-red-contrib-string
The string node will appear in the palette under the function group.