0

I am trying to search in a string and trying to extract a particular thing from the string. here are the example i am trying to solve.

const string = 'xtyzjdjgdjf +91888123455, +918885558565 +916885123456, +911234569870'
i am trying to extract only '+91888123455, +918885558565 +916885123456, +911234569870'

but the number is dynamic and it changes as per the response

6
  • How do you know what you would like to extract? Commented Jun 30, 2020 at 17:37
  • @Rastalamm i just want to extract the numbers from the string and these numbers will be dynamic Commented Jun 30, 2020 at 17:42
  • @luciferluci ... and how about the optional commas/whitespaces? From how the Q. was presented it looks like this stuff should be preserved exactly as it comes with the original input. Commented Jun 30, 2020 at 18:32
  • @PeterSeliger i am expecting these types of inputs from the users may be they put commas/ and maybe not that's why i have added in this way. Commented Jun 30, 2020 at 18:40
  • @luciferluci ... "... may be they put commas/ and maybe not ..." ... but does this mean one needs to preserve this meta information or would one not be better off with suppressing it and just go with the pure number? Commented Jun 30, 2020 at 19:36

5 Answers 5

2

about the pattern of the used regex ... /\+\d+,{0,1}/g ...

  1. \+ ... match a single mandatory + sign ... followed by ...
  2. \d+ ... at least one digit (or a sequence of digits) ... followed by ...
  3. ,{0,1} ... a single but optional comma.
  4. g ... search/match the pattern globally.

function extractValidNumberSequences(str) {
  return str.match(/\+\d+,{0,1}/g).join(' ');
}

const test = `
  xtyzjdjgdjf +91888123455, +918885558565 +916885123456,,, +91123456987 dsjk jjd
  sag sadgsadj 43865984 dsjghj, +918885558565 +916885123456,,, +91123456987 dsjk
`;

console.log(`extractValidNumberSequences(test) : "${ extractValidNumberSequences(test) }"`);
.as-console-wrapper { min-height: 100%!important; top: 0; }

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

Comments

0

The easiest way is using Regex: /\+\d*/g. This Regex is searching for all the strings that starts with + and have 0 or more numbers after that.

const string = 'xtyzjdjgdjf +91888123455, +918885558565 +916885123456, +911234569870';

const result = [...string.matchAll(/\+\d*/g)] // Get all the regex matches
  .map(([text]) => text) // Grabbing the first element in the array which is the text
  .join(','); // Join the text together

console.log(result);

Resources

3 Comments

This will return a match even if the substring is not present.
Or just: js const string = 'xtyzjdjgdjf +91888123455, +918885558565 +916885123456, +911234569870'; const result = string.match(/\+\d*/g).join(','); console.log(result);
@baradm100 ... this approach/regex doesn't cover the OP's need (at least it looks like it from the Q.) of preserving commas/whitespaces as provided with the original string ... OP wants '+91888123455, +918885558565 +916885123456, +911234569870'.
0

var stringVal = "'xtyzjdjgdjf +91888123455, +918885558565 +916885123456, +911234569870".replace('xtyzjdjgdjf','');

console.log(stringVal); //prints: +91888123455, +918885558565 +916885123456, +911234569870

For all occurrences to be discarded use: Eg -- var ret = "data-123".replace(/data-/g,''); PS: The replace function returns a new string and leaves the original string unchanged, so use the function return value after the replace() call.

Comments

0

Okay, let me get this straight: You're looking to extract a sequence of numbers from a string beginning in a "+" symbol and ending in a "," character?

The way you could do that is looping through the string.

let Raw = "udhaiuedh +34242, +132354"

function ExtractNumbers(ToDecode) {
    let StartSet = ["+"]
    let EndSet = [","," "]
    let Cache = ""
    let Finished = []
    
    for (Char of ToDecode) {
        if (StartSet.includes(Char)) {
            if (Cache.length != 0) {
                Finished.push(Cache)
            }
            Cache = ""
        } else if (EndSet.includes(Char)) {
            if (Cache.length != 0) {
                Finished.push(Cache)
            }
            Cache = ""
        } else {
            if (Number(Char)) {
                Cache = Cache + String(Char)
            }
        }
    }
    if (Cache.length != 0) {
        Finished.push(Cache)
    }
    return Finished
}

console.log(ExtractNumbers(Raw))

It's not perfect, but a nice example to get you started :)

Comments

0
const string = 'xtyzjdjgdjf +91888123455, +918885558565 +916885123456, +911234569870'
const extractMe = '+91888123455, +918885558565 +916885123456, +911234569870'
    
if (string.includes(extractMe)) {
   // extractMe is what you want to extract
}

1 Comment

sorry but these numbers in the string will be dynamic

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.