Jaqueline
Jaqueline

Reputation: 33

Getting second digit in a string using Javascript + Regex?

I'm wondering how I can get the second digit of a string where we don't know the number of digits the second number will be and without using splice or substring.

Ex. Channel.0.This.13

Should Return: 13

I've seen a few similar questions but they

  1. typically know the number of digits the second number will be or
  2. use splicing and substring, which I do not want to use in this case.

I appreciate the help :)

Upvotes: 0

Views: 974

Answers (7)

dale landry
dale landry

Reputation: 8600

You can reference the regex .match() key. str.match(reg)[1]

const str1 = 'Channel.0.This.13'
const str2 = 'some.otherStrin8..'
const str3 = '65 people.For.&*=20.them,98'
const regex = /\d+/g

function matchSecond(str, reg) {
  str.match(reg)[1] ? output = str.match(reg)[1] : output = false
  return output;
}

console.log(matchSecond(str1,regex))
console.log(matchSecond(str2,regex))
console.log(matchSecond(str3,regex))

Upvotes: 0

Yurii Hierts
Yurii Hierts

Reputation: 1920

It will help .*?\d+.*?(\d+).*$

"Channel.0.This.13.Channel.0.This.56".match(/.*?\d+.*?(\d+).*$/).pop()
// Output: 13
"Channel.0.This.13".match(/.*?\d+.*?(\d+).*$/).pop()
// Output: 13

Upvotes: 0

user6386114
user6386114

Reputation:

String.prototype.match() returns an array whose contents depend on the presence or absence of the global (g) flag, or null

const input1 = "Channel.0.This.13",
  input2 = "Channel.0.This",
  input3 = "Channel.This.";

const digitMatch = function (input) {
  const digits = input.match(/\d+/g);
  return (digits && digits[1]) || "Not Found";
};

console.log(digitMatch(input1));
console.log(digitMatch(input2));
console.log(digitMatch(input3));

if no matches are found.

Upvotes: 0

sunil Prajapat
sunil Prajapat

Reputation: 89

Assuming the original string contains only alphabets, numbers and '.' (in between),

Here is my solution (Pseudo code):

String givenString;

regex=/([0-9]+)(\.[a-zA-Z]+)?(\.[0-9]+)/;

//below code will return an array or null (if no second number is present)

match=givenString.match(regex);

//access last element of array. It will be like '.13' , just remove '.' and you are good to go

match.pop()

Javascript Regex Docs:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Groups_and_Ranges

Upvotes: 0

eamanola
eamanola

Reputation: 434

try this:

^[^\d]*\d+[^\d]+(\d+).*

Example:

const secondDigit = "Channel.0.This.13".match(/^[^\d]*\d+[^\d]+(\d+).*/).pop();
 
console.log(Number(secondDigit)); // 13

Upvotes: 0

Yurii Hierts
Yurii Hierts

Reputation: 1920

Use this regex (\d*)$. This will return only group with numbers which in the end of the string.

Upvotes: 0

hgb123
hgb123

Reputation: 14881

You could use String.prototype.match

In case that the string does not have any number, which matches will return null, you should use optional chaining ?. for a safer array index access

const str = "Channel.0.This.13";
const res = str.match(/\d+/g)?.[1];

console.log(res);

Upvotes: 1

Related Questions