- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2024 06:08 AM
Hi All,
Can you tell me how store Last word one of the variable :
short Description :HR Profile GET API failure for cili
In this case i want to store Cili in on of the variable, Can you help me .
Thanks and Regards,
chandan
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2024 04:33 AM
You're not invoking the function correctly. Have you used JavaScript previously to ServiceNow?
Correct usage:
function getLastWord(str) {
// Trim any whitespace from the ends of the string
str = str.trim();
// Split the string into an array of words
var words = str.split(/\s+/);
// Return the last word
return words[words.length - 1];
}
getLastWord(current.getValue('short_description'));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2024 06:57 AM - edited 06-07-2024 06:58 AM
You would need to use Regex to capture the last word in a string, something like
var regex = /\w+$/
var test = 'This is a test string';
var match = test.match(regex)
gs.info(match)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2024 07:42 PM
Thank you replay
this script is not working :
var regex = /\w+$/;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2024 07:09 AM
You can create a simple function for this:
function getLastWord(str) {
// Trim any whitespace from the ends of the string
str = str.trim();
// Split the string into an array of words
var words = str.split(/\s+/);
// Return the last word
return words[words.length - 1];
}
// Example usage:
var sentence = "Hello, this is a sample sentence.";
getLastWord(sentence);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2024 07:47 PM
This script is not working :
