Regex

kali
Tera Contributor

Hi All,

I am using regex to get a aphanumeric or numer from a string . I am using regex

 /[a-zA-Z0-9]*\d+[a-zA-Z]/g; however it is returning only aphanumeric but ignoring numbers in the string .Please help me to get both aphanumeric or numeric value from a string.
 
Thanks in advance
1 ACCEPTED SOLUTION

AmitPandey_0-1722579908585.png

var str1 = "this is a test r11MS0AB8";
var str2 = "this is a test 123Abc12CD";
var str3 = "this is a test 51571008";

var regex = /\b(?=[a-zA-Z0-9]*\d)[a-zA-Z0-9]+\b/;

var match1 = str1.match(regex);
var match2 = str2.match(regex);
var match3 = str3.match(regex);

gs.print(match1 ? match1[0] : 'No match'); // Output: r11MS0AB8
gs.print(match2 ? match2[0] : 'No match'); // Output: 123Abc12CD
gs.print(match3 ? match3[0] : 'No match'); // Output: 51571008

View solution in original post

23 REPLIES 23

Robbie
Kilo Patron
Kilo Patron

Hi @kali,

 

Latest update so not lost in the thread:

Use the below regex to extract the numeric characters from the String:

 

var str = "this is a test 5005123";
var rgr = /(\d+)/g;
var data =str.match(rgr);
gs.print(data);

Result: 5005123

 

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.



Thanks, Robbie

kali
Tera Contributor

Hi @Robbie ,

I need a regex to get the alphanumeric value or numeric value from the string . The string will contain either alphanumeric or numeric but not both in the string. I need the regex to capture the alphanumeric not separate it . Please suggest such a regex 

kali
Tera Contributor

and @Robbie  i am expecting a single regex to for both the use cases

Hi @kali,

 

Can you provide an example string with the expected results please.

 

it’s not completely clear what you’re trying to achieve, hence an example with expected results will help.

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.



Thanks, Robbie

kali
Tera Contributor

@Robbie  I need to write a code to get a value from the string that is passed to ServiceNow.  To get the value i am trying to use regex to get the value

var str = "this is a test 5005123";

In the above string i need to get the value 5005123

 

var str = "this is a test 123Abc12CD";

In the above string using regex i need to get the value of 123Abc12CD