0

I have string:

=?windows-1256?B?IObH4cPM5dLJIA==?= =?windows-1256?B?x+HYyO3JIC4uLg==?= =?windows-1256?B?LiDH4djj5s3Hyg==?= =?windows-1256?B?Rlc6IOTP5skgKA==?=

I need to extract all matches between ?B? and ==?=.

As a result I need:

IObH4cPM5dLJIA

x+HYyO3JIC4uLg

LiDH4djj5s3Hyg

Rlc6IOTP5skgKA

P.S. This string is taken from textarea and after function executed, script should replace current textarea value with result. I've tried everything,

var result = str.substring(str.indexOf('?B?')+3,str.indexOf('==?='));    

Works almost the way I need, but it only finds first match. And this doesn't work:

function Doit(){
    var str = $('#test').text();
    var pattern = /(?B?)([\s\S]*?)(==?=)/g;
    var result = str.match(pattern);

    for (var i = 0; i < result.length; i++) {
        $('#test').html(result);
    };
}
Community
  • 1
  • 1
Artjom
  • 37
  • 4

2 Answers2

0

? has a special meaning in regex which matches preceding character 0 or 1 time..

So, ? should be escaped with \?


So the regex should be

(?:\?B\?)(.*?)(?:==\?=) 

[\s\S] has no effect and is similar to .

Anirudha
  • 32,393
  • 7
  • 68
  • 89
  • Thanks. That's helpful and almost does what I need. Now it's generates: `?B?IObH4cPM5dLJIA==?=?B?x+HYyO3JIC4uLg==?=?B?LiDH4djj5s3Hyg==?=?B?Rlc6IOTP5skgKA==?=` How do I remove ?B? and ==?= from the result and how to add a linebreak between results? – Artjom Feb 01 '13 at 12:16
  • @Artjom you need to access group 1's value..[here's](http://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regex) how it is done – Anirudha Feb 01 '13 at 12:23
  • Oh, I've done it. Maybe it's not an elegant way, however it works :) function Doit(){var str = $('#test').text();var pattern = /(\?B\?)(.*?)(==\?=)/g;var result str.match(pattern); $('#test').html(result); var cont = $('#test').text(); var final = cont.replace(/(\?B\?)/g,''); var final2 = final.replace(/(==\?=)/g,'
    '); $('#test').html(final2);}
    – Artjom Feb 01 '13 at 12:39
0

The metacharacter ? needs escaping, i.e. \? so it is treated as a literal ?.

[\s\S] is important as it matches all characters including newlines.

var m,
    pattern = /\?B\?([\s\S]*?)==\?=/g;

while ( m = pattern.exec( str ) ) {
    console.log( m[1] );
}

// IObH4cPM5dLJIA
// x+HYyO3JIC4uLg
// LiDH4djj5s3Hyg
// Rlc6IOTP5skgKA

Or a longer but perhaps clearer way of writing the above loop:

m = pattern.exec( str );

while ( m != null ) {
    console.log( m[1] );
    m = pattern.exec( str );
}

The String match method does not return capture groups when the global flag is used, but only the full match itself.

Instead, the capture group matches of a global match can be collected from multiple calls to the RegExp exec method. Index 0 of a match is the full match, and the further indices correspond to each capture group match. See MDN exec.

MikeM
  • 13,156
  • 2
  • 34
  • 47