Мастер-классы по Javascript Екатеринбург Ростов-на-Дону Москва Узнать больше...
Содержание (скрыть) Содержание (показать)

Match a string with quotes

Write a regular expression to match a double-quoted string.

The string may have escaped quotes \" inside.

For example:

str = ' .. "test" .. '   // matches "test"

// Slashes are escaped 
str = ' .. "say \\"Hello\\"!" .. ' //  "say \"Hello\"!"

str = ' .. "\\" .. '  // no match

Решение
Решение

The solution is /"(\\.|[^"\\])*"/.

A backslashed symbol \\. (quote or anything) is consumed before non-backslashed match attempt [^"\\].

var re = /"(\\.|[^"\\])*"/g

alert( ' .. "test" .. '.match(re) ) 

alert( ' .. "say \\"Hello\\"!" .. '.match(re) )

alert( ' .. "\\" .. '.match(re) )

#287
Наверх

Реклама

Нашли опечатку?

Нашли опечатку на сайте? Что-то кажется странным?
Выделите соответствующий текст и нажмите Ctrl+Enter!

Последние Комментарии

Помоги другим!

Помоги другим узнать о хорошей статье!