Script to find whole word within a string.

dfry123
Mega Expert

Hello,

I'm looking for help on how I can find a whole word within a string using a before business rule. Basically here's my scenario:

If the word "test" is in the short description field the business rule needs to run. I am not able to get it to only find "test".. it will bring back "testing" and "dtesting" as an example.

The script I have is below.. any help would be appreciated!! I just have it looking for test and displaying the message for testing purposes..

(function executeRule(current, previous /*null when async*/) {
var ste = current.short_description.toString("test");

  if(ste.indexOf("test") > -1){
   
      current.u_business_development = 'true';
      gs.addInfoMessage("test is there");
      }
})(current, previous);

7 REPLIES 7

Hi Rahul,

what would the regex be if I wanted to pull the entire string that starts with a word?

So my string is:

 

Notification time : 2021-10-09 16:58:09.035

Message : DB instance restarted

Event ID : http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_Events.html#RDS-EVENT-0006

 

And I want to find the last 4 characters in the line that starts with "Event ID".

So for this example, I want to return "0006" because that line starts with "Event ID"

is this possible with regex?  

thanks!

I ended up figuring this out...but if anyone sees a better way certainly chime in.  I used 2 regex expressions to first get my line, then my last 4 digits.

var statusInput = "This is my string and it has several lines like in a body of an email.  Event ID - this also part of my string and it ends with 0006";

var textOne = statusInput;
var pattern = /Event ID(.*)/;
var txtParse = pattern.exec(textOne);
var pattern2 = /.{4}$/;
var txtParse2 = pattern2.exec(txtParse);

output from this:  txtParse2 = "0006"

 

 

saurav1
Kilo Expert

Hey Darrel,



Just take the entire string in an array.


1. var a=" Your String";


Split the array with blank spaces.


2. a.split(' ');


Check which position contains the word string.


3.for(int i=0;i<a.length();i++)


{


if(a[i]=="string")


return (a[i+1]);



}else


return 0;


Darrell Fry wrote:



Hello,



I'm looking for help on how I can find a whole word within a string using a before business rule. Basically here's my scenario:



If the word "test" is in the short description field the business rule needs to run. I am not able to get it to only find "test".. it will bring back "testing" and "dtesting" as an example.



The script I have is below.. any help would be appreciated!! I just have it looking for test and displaying the message for testing purposes..




(function executeRule(current, previous /*null when async*/) {
var ste = current.short_description.toString("test");


if(ste.indexOf("test") > -1){

current.u_business_development = 'true';
gs.addInfoMessage("test is there");
}
})(current, previous);