0

I want to get the string after =" and before " eg - unSelAll('FBtest11_6',this.value,this.id)

some times the =" " might be =''

eg - onclick ='unSelAll("FBtest11_6",this.value,this.id)'

I want the string starting from function name only

Pedram
  • 6,256
  • 10
  • 65
  • 87

1 Answers1

1

Description

This regex will do the following:

  • find the first instance of onclick = and return the associated value
  • allow the value to be surrounded in single or double quotes, or no quotes
  • allows a space between onclick and it's associated =
  • Since this appears to be pattern matching in an HTML type of string, the regex will avoid may difficult edge cases with pattern matching HTML

Regex

(?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*?\s*\Konclick\s*=(['"]?)(.*?)\1(?:\s|$)

Regular expression visualization

Capture Groups

  • Capture group 0 gets the entire string onclick string includeing the value and surrounding quotes
  • Capture group 1 gets the quote around the value if it existed
  • Capture group 2 gets the onclick value not including any quotes

Examples

Sample Text

Note the difficult edge case in the second line

onclick ='unSelAll("FBtest11_6",this.value,this.id)'
onmouseover=" onclick ='This is not the droid you are looking for' " onclick ="find me instead"

Sample Matches

[0][0] = onclick ='unSelAll("FBtest11_6",this.value,this.id)'

[0][1] = '
[0][2] = unSelAll("FBtest11_6",this.value,this.id)

[1][0] = onclick ="find me instead"
[1][1] = "
[1][2] = find me instead

Live Example

https://regex101.com/r/yU8eL1/2

Explanation

NODE                     EXPLANATION
----------------------------------------------------------------------
  (?:                      group, but do not capture (0 or more times
                           (matching the least amount possible)):
----------------------------------------------------------------------
    [^>=]                    any character except: '>', '='
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    ='                       '=\''
----------------------------------------------------------------------
    [^']*                    any character except: ''' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
    '                        '\''
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    ="                       '="'
----------------------------------------------------------------------
    [^"]*                    any character except: '"' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
    "                        '"'
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    =                        '='
----------------------------------------------------------------------
    [^'"]                    any character except: ''', '"'
----------------------------------------------------------------------
    [^\s>]*                  any character except: whitespace (\n,
                             \r, \t, \f, and " "), '>' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )*?                      end of grouping
----------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  \K                       'K'
----------------------------------------------------------------------
  onclick                  'onclick'
----------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  =                        '='
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    ['"]?                    any character of: ''', '"' (optional
                             (matching the most amount possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
  \1                       what was matched by capture \1
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
----------------------------------------------------------------------
  )                        end of grouping
Community
  • 1
  • 1
Ro Yo Mi
  • 14,790
  • 5
  • 35
  • 43