Loading...
n.velchev95 avatar n.velchev95 79 Точки

[Technical Issue] JavaScript Regex - Може ли някой да обясни подробно как точно работи Regex кода от изпита на 27.07.2014?

 Може ли някой да обясни подробно как точно работи Regex кода от изпита на 27.072014 ? Би било полезно. 

  var regex = /<a\s+([^>]+\s+)?href\s*=\s*('([^']*)'|"([^"]*)|([^\s>]+))[^>]*>/g;


  http://pastebin.com/A4ySA6cV

Тагове:
0
JavaScript Fundamentals
genov1824 avatar genov1824 54 Точки

http://www.lynda.com/Regular-Expressions-tutorials/Using-Regular-Expressions/85870-2.html

 

Ето този курс е много полезен. Има го по торент сайтовете, препоръчвам ти да си го свалиш и изгледаш. Защото обяснението на този regex ще отнеме доста време, и незнам дали ще успея да ти го обясня, така че да го разбереш, след този курс ще го разбереш с 1 поглед. 

Поздрави

4
dimitarstoyanov90 avatar dimitarstoyanov90 164 Точки
NODE                     EXPLANATION
--------------------------------------------------------------------------------
  /<a                      '/<a'
--------------------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  (                        group and capture to \1 (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    [^>]+                    any character except: '>' (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    \s+                      whitespace (\n, \r, \t, \f, and " ") (1
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )?                       end of \1 (NOTE: because you are using a
                           quantifier on this capture, only the LAST
                           repetition of the captured pattern will be
                           stored in \1)
--------------------------------------------------------------------------------
  href                     'href'
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  =                        '='
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  (                        group and capture to \2:
--------------------------------------------------------------------------------
    '                        '\''
--------------------------------------------------------------------------------
    (                        group and capture to \3:
--------------------------------------------------------------------------------
      [^']*                    any character except: ''' (0 or more
                               times (matching the most amount
                               possible))
--------------------------------------------------------------------------------
    )                        end of \3
--------------------------------------------------------------------------------
    '                        '\''
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    "                        '"'
--------------------------------------------------------------------------------
    (                        group and capture to \4:
--------------------------------------------------------------------------------
      [^"]*                    any character except: '"' (0 or more
                               times (matching the most amount
                               possible))
--------------------------------------------------------------------------------
    )                        end of \4
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    (                        group and capture to \5:
--------------------------------------------------------------------------------
      [^\s>]+                  any character except: whitespace (\n,
                               \r, \t, \f, and " "), '>' (1 or more
                               times (matching the most amount
                               possible))
--------------------------------------------------------------------------------
    )                        end of \5
--------------------------------------------------------------------------------
  )                        end of \2
--------------------------------------------------------------------------------
  [^>]*                    any character except: '>' (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  >                        '>'
7
Anita avatar Anita 360 Точки

Можеш да използваш този сайт http://regex101.com/  ... Въвеждаш твоя regex в полето "REGULAR EXPRESSION" и след това в дясно има подробни обяснения в "EXPLANATION".

За твоя случай ето какво излиза....

/<a\s+([^>]+\s+)?href\s*=\s*('([^']*)'|"([^"]*)|([^\s>]+))[^>]*>/g


<a matches the characters <a literally (case sensitive)
\s+ match any white space character [\r\n\t\f ]
Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]


1st Capturing group ([^>]+\s+)?
Quantifier: Between zero and one time, as many times as possible, giving back as needed [greedy]
Note: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
[^>]+ match a single character not present in the list below
Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]
> a single character in the list > literally (case sensitive)
\s+ match any white space character [\r\n\t\f ]
Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]
href matches the characters href literally (case sensitive)
\s* match any white space character [\r\n\t\f ]
Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
= matches the character = literally
\s* match any white space character [\r\n\t\f ]
Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]


2nd Capturing group ('([^']*)'|"([^"]*)|([^\s>]+))
1st Alternative: '([^']*)'
' matches the character ' literally


3rd Capturing group ([^']*)
[^']* match a single character not present in the list below
Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
' the literal character '
' matches the character ' literally


2nd Alternative: "([^"]*)
" matches the character " literally


4th Capturing group ([^"]*)
[^"]* match a single character not present in the list below
Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
" the literal character "


3rd Alternative: ([^\s>]+)


5th Capturing group ([^\s>]+)
[^\s>]+ match a single character not present in the list below
Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\s match any white space character [\r\n\t\f ]
> a single character in the list > literally (case sensitive)
[^>]* match a single character not present in the list below
Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
> a single character in the list > literally (case sensitive)
> matches the characters > literally
g modifier: global. All matches (don't return on first match)

 

1
01/09/2014 22:08:57
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.