1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

javascript regexp capture match index substring

Discussion in 'JavaScript' started by weavercrowds, Jul 8, 2020.

  1. #1
    In this example I want capture the fourth hide in S is hide3;

    var S="gwrtgwhide0regwtrghide1erthgrthide2rtgbwtghide3ertghtrwhide4wertgerghide5ertghwrtghide6ewrtghwrtghide7wertgqew";
    
    var j=/^((?:((?!hide\d).)*hide\d)){3}(?:((?!hide\d).)*(hide\d))/.exec(S);
    
    console.log(j[j.length-1]);
    Code (JavaScript):
    I can to make a function for this in native javascript - ecmascript6

    
    window["getIndexValue"]=(...a)=>{
         let string=a[0],substring=a[1],index=a[2];
         let R=new RegExp("^((?:((?!"+substring+").)*"+substring+")){"+index+"}(?:((?!"+substring+").)*("+substring+"))");
         let j=R.exec(string);
         return j[j.length-1];
    }
    console.log(getIndexValue("jhfisd5hdsfh5jhsodf5zdfgdf5fgsd5sdgsd","5[a-z]",3));
    //return 5f
    console.log(getIndexValue("jhfisd5hdsfh5jhsodf5zdfgdf5fgsd5sdgsd","5[a-z]",2));
    //return 5z
    
    Code (JavaScript):
     
    weavercrowds, Jul 8, 2020 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    Is this a question or a statement? Either way your regex looks painfully and pointlessly convoluted, and the single letter non-descriptive variables aren't doing anyone any favors... much less the global assignment of the painfully cryptic arrow function trash --- at which point just declare a bloody function. Same for the silly use of spread when you've only got one parameter that's already iterable.

    Are you trying to do something like this?

    
    var
    	testString = 'gwrtgwhide0regwtrghide1erthgrthide2rtgbwtghide3ertghtrwhide4wertgerghide5ertghwrtghide6ewrtghwrtghide7wertgqew';
    	
    function findNumberSuffixedInString(needle, haystack, offset) {
    	var result = haystack.match(new RegExp('('+ needle + '\\d+)','g'));
    	if (!Number.isInteger(offset)) return result;
    	if (offset < 0) offset += result.length;
    	return result[offset];
    }
    
    // full array
    console.log(findNumberSuffixedInString('hide', testString));
    // unique index from beginning == hide3
    console.log(findNumberSuffixedInString('hide', testString, 3));
    // unique index from end == hide5
    console.log(findNumberSuffixedInString('hide', testString, -3));
    
    Code (markup):
     
    Last edited: Jul 9, 2020
    deathshadow, Jul 9, 2020 IP
  3. weavercrowds

    weavercrowds Peon

    Messages:
    5
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    3
    #3
    If the truth is that you are right to everything, but I wanted to teach with this to match from the same regular expression without the use of regexp.match from javascript but by executing the regular expression itself by using the excluding tags of (? :) i negation of the own exclusion ((?! word).) *, thus demonstrating the potential of regular expressions, since not everyone knows their potential. I also like to use arrow functions to teach another way of doing functions, basically showing parts of ECMASCRIPT6 and so people see the versatility of javascript. It may be more poorly done or well done, that depends, but the fact is that if you had wanted to index a needle from javascript, it could have been done in many ways, all of them more or less valid according to one programmer or another. If I wanted to just use javascript it would have been as easy as just using split with initial or final needles variants. In any case I appreciate your point of view, that helps me improve
     
    weavercrowds, Jul 9, 2020 IP