Search code examples
javascriptregexdata-extraction

How to extract only numbers from string except substring with special character in beginning of numbers


my plan is extract numbers from string except numbers with special character. What I mean? Please imagine a following (like Excel formula):

=$A12+A$345+A6789

I need to extract numbers where in beginning of them doesn't exist any character $, so result of right regex should be:

12
6789

I made some investigation where I used a following regex:

/[A-Z][0-9]+(?![/W])/g

which extracts:

A12
A6789

I was thinking to use nested regex (to extract numbers from that result additionally) but I have no idea if it possible. My source code in javascript so far:

http://jsfiddle.net/janzitniak/fvczu7a0/7/

Regards

Jan


Solution

  • const regex = /(?<ref>\$?[A-Z]+(?<!\$)[0-9]+)/g;
    const str = `=$A12+A$345+A6789`;
    
    const refs = [...(str.matchAll(regex) || [])].map(result => result.groups.ref);
    
    console.log(refs)
    

    Matches any string containing A-Z once or more that is preceded by a $ zero or one times, followed by 0-9 once or more but not preceded by a $, all followed by + zero or one times.

    You ignore all matched groups, but capture the one you want, referenced as ref (you can call it whatever you want).

    Output:

    ["$A12","A6789"]
    

    If you want just the number part, you can use:

    const regex = /\$?[A-Z]+(?<!\$)(?<num>[0-9]+)/g;
    const str = `=$A12+A$345+A6789`;
    const nums = [...(str.matchAll(regex) || [])].map(result => +result.groups.num);
    console.log(nums)
    

    Output:

    [12, 6789]