1

So I need to pull a number value from a string. I currently have a working solution but I feel that maybe I can improve this using a regular expression or something.

Here is my working solution

var subject = "This is a test message [REF: 2323232]";

if(subject.indexOf("[REF: ") > -1){
  var startIndex = subject.indexOf("[REF: ");
  var result = subject.substring(startIndex);
  var indexOfLastBrace = result.indexOf("]");
  var IndexOfRef = result.indexOf("[REF: ");
  var ticketNumber = result.substring(IndexOfRef + 6, indexOfLastBrace);

  if(!isNaN(ticketNumber)){
    console.log("The ticket number is " + ticketNumber)
    console.log("Valid ticket number");
  }
  else{
    console.log("Invalid ticket number");
  }
}

As you can see I'm trying to pull the number value from after the "[REF: " string.

2
  • 3
    Simply do \[REF: *(\d+) in a match(): subject.match(/\[REF: *(\d+)/)[1] Commented Feb 16, 2018 at 17:54
  • 1
    You could also do +subject.split("[REF: ")[1].slice(0,-1); Commented Feb 16, 2018 at 17:57

4 Answers 4

2

// Change of the text for better test results
var subject = "hjavsdghvwh jgya 16162vjgahg451514vjgejd5555v fhgv f 262641hvgf 665115bs cj15551whfhwj511";
var regex = /\d+/g;
let number = subject.match( regex )
console.log(number)

It Will return array for now, and if no match found, it will return null. For most of the time, when i used this regex i get perfect result unless if string contains decimal values.

Sign up to request clarification or add additional context in comments.

4 Comments

true but if they have numbers inside of the message it kind of ruins it.
Thank you for the quick response. This works but as zfrisch said above if I have another number in the string it gets that first.
actually not getting the issue, i have changed the string now and it contains more number values and it is returning array of numbers. Put some example so i could check it for now
@satyampathak What you said is true. I get an array of numbers but I only want the number between [REF: 23232]. I don't want the other numbers.
1

var str = 'This is a test message [REF: 2323232]'

var res = str.match(/\[REF:\s?(\d+)\]/, str)

console.log(res[1])

1 Comment

This works handles the case when I have additional numbers in the string. So if I have 'This is a test 34444 message [REF: 22222]' this returns 22222. Thanks!
1

If you don't want to use a regular expression (I tend to stay away from them, even though I know they are powerful), here is another way to do it:

// Your code:
/*var subject = "This is a test message [REF: 2323232]";

if(subject.indexOf("[REF: ") > -1){
  var startIndex = subject.indexOf("[REF: ");
  var result = subject.substring(startIndex);
  var indexOfLastBrace = result.indexOf("]");
  var IndexOfRef = result.indexOf("[REF: ");
  var ticketNumber = result.substring(IndexOfRef + 6, indexOfLastBrace);

  if(!isNaN(ticketNumber)){
    console.log("The ticket number is " + ticketNumber)
    console.log("Valid ticket number");
  }
  else{
    console.log("Invalid ticket number");
  }
}*/

// New code:
const subject = "This is a test message [REF: 2323232]";
const codeAsString = subject.split('[REF: ')[1]
  .split(']')
  .join('');

if (!isNaN(parseInt(codeAsString))) {
  console.log('Valid ticket number: ', parseInt(codeAsString));
}
else {
  console.log('Invalid ticket number: ', codeAsString);
}

1 Comment

Interesting take on the solution. Just need to put in some checks if the [REF: 454545 ] is not there. Other than that its a valid solution. Thanks.
1

This will extract number

var subject = "This is a test message [REF: 2323232]";
var onlyNum = subject.replace(/.*(:\s)(\d*)\]$/,'$2');
console.log(onlyNum)

Here, same but the number is now a real int

var subject = "This is a test message [REF: 2323232]";
var onlyNum = parseInt(subject.replace(/.*(:\s)(\d*)\]$/,'$2'));
console.log(onlyNum)

1 Comment

Thank you for the quick response. If I have "This is a test 3333 message [REF: 2323232]" it will return 33332323232 where I only want 2323232.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.