0

I have a javascript code, and I wanna get a specific function. my code is working well, but I have a problem how to get just the first corresponding of a string. in my example it is 'false', here is the javascript code : 'the code is not 100% correct , just an example.'

$(document).ready(function() {
        $('#flash').insertAfter($('.report-header'));
        $('a.bounce-tip').tooltipster({interactive: true, position: 'top', trigger: 'click'});
        $('#add-manual-rejection').dialog({
            autoOpen: false,
            resizable: false,
            width: 400,
            modal: true
        });

        $('#add-blacklist-show').click(function() {
            $('#add-manual-rejection').find('input[name=address], input[name=comment]').val('');
            $('#add-manual-rejection').dialog('open');
            return false;
        });

        $('#cancel-add-rejection').click(function() {
            $('#add-blacklist-show').click(function() {
                $('#add-manual-rejection').find('input[name=address], input[name=comment]').val('');
                 $('#add-manual-rejection').dialog('open');
                return false;
               });
          return false;
       });
    });

and Now my regex is :

/([\(\'\$\#]+add-b[\w\W]+false)/

I also tried to use {1,2,etc....} to get the first result. so it became like this :

([\(\'\$\#]+add-b[\w\W]+false{1})

But in this case the interpreter give the result just for the last letter which is 'e' not for the whole word.

I am using this website to match my regex : http://rubular.com/

any advices !? thanks :)

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Rabih
  • 298
  • 1
  • 6
  • 18
  • 1
    Your quantifier after false will only refer to the last letter – Ron Rosenfeld Feb 04 '14 at 17:55
  • I note you edited your post to indicate javascript and not nsregularexpressions. In javascript, the "escapes" within the character class are all superfluous (although they should not change how it is interpreted). – Ron Rosenfeld Feb 04 '14 at 18:03

2 Answers2

2

I'm not entirely clear on what you want to return. If what you want to return is the first substring in the code you posted that starts with one character in the set of ('$# followed by add-b and ended with the word false, you need to make [\w\W]+ 'non-greedy'. So your regex would be:

[('$#]+add-b[\w\W]+?false

I see you have edited your tags to show javascript and not nsregularexpressions. A code example of the above might look like:

var myregexp = /[('$#]+add-b[\w\W]+?false/;
var match = myregexp.exec(subject);
if (match != null) {
    result = match[0];
} else {
    result = "";
}
Ron Rosenfeld
  • 53,870
  • 7
  • 28
  • 60
1
/(\$\('#add-b(?:(?!}\);)[\s\S])+}\);)/
# http://rubular.com/r/7DzzH82kiH
# Match 1
# 1. $('#add-blacklist-show').click(function() {
# $('#add-manual-rejection').find('input[name=address], input[name=comment]').val('');
# $('#add-manual-rejection').dialog('open');
# return false;
# });
# Match 2
# 1. $('#add-blacklist-show').click(function() {
# $('#add-manual-rejection').find('input[name=address], input[name=comment]').val('');
# $('#add-manual-rejection').dialog('open');
# return false;
# });

http://rubular.com/r/7DzzH82kiH

regex matches each instance of code block within string

SoAwesomeMan
  • 3,226
  • 1
  • 22
  • 25
  • If he's using javascript (and I'm not sure since I now see that he was not the one that edited the tags), then I do not think that dot matches newline is an option. – Ron Rosenfeld Feb 04 '14 at 18:29
  • @RonRosenfeld Good call. I will update my answer to use [\s\S] as the multiline matcher so the the regex will work in javascript as well. – SoAwesomeMan Feb 05 '14 at 06:42
  • thanks, it works great, would you please explain a bit your regex. – Rabih Feb 05 '14 at 08:49
  • 1
    @Rabi opening combo jquery selector: `\$\('` char combo identifies target: `#add-b` start [non-capturing group](http://stackoverflow.com/questions/3512471/non-capturing-group): `(?:` selector closing combo [negative lookahead](http://stackoverflow.com/a/5332750/1076207): `(?!}\);)` ['placed before'](http://stackoverflow.com/a/2646343/1076207) this [javascript multiline matcher](http://stackoverflow.com/a/16119722/1076207): `[\s\S]` match every char before negative lookahead: `)+` stop negative lookahead from being greedy (itself is not before itself): `}` added for reuse or aesthetics: `\);` – SoAwesomeMan Feb 05 '14 at 09:57
  • @Rabi try this regex with your text to see more examples of a non‑capturing group and negative lookahead: `/\b((?:(?!false|true)[\w\s:]+)),/` ➦ http://rubular.com/r/7DzzH82kiH – SoAwesomeMan Feb 05 '14 at 10:03
  • @awesome thanks for your response I really appreciate it. I will read more about non-capturing group. – Rabih Feb 05 '14 at 10:27
  • 1
    @Rabi actually, try this one instead `/\b(?:(?!false|true)+)(\w*:\s*true|\w*:\s*false)/` ➦ http://rubular.com/r/7NDPGfUvH1 – SoAwesomeMan Feb 06 '14 at 18:47