MindGame
MindGame

Reputation: 1251

Pull number value from string

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.

Upvotes: 1

Views: 232

Answers (4)

Richard
Richard

Reputation: 1048

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)

Upvotes: 1

th3n3wguy
th3n3wguy

Reputation: 3747

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);
}

Upvotes: 1

Satyam Pathak
Satyam Pathak

Reputation: 6932

// 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.

Upvotes: 2

Srdjan M.
Srdjan M.

Reputation: 3405

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

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

console.log(res[1])

Upvotes: 1

Related Questions