單引號簡單的說會找到「引號中的引號」,這在HTML中是很常見的。搞不懂?看下去就懂了…。
(請參照原文)
The single quote is smarter than your average quote:
It attempts to match the appropriate ending quote for
any quote previously matched by the double quote -
even if there are other quotes in between!
如:
雙引號中的單引號
href=" javascript:window.open( ' bison.html ' ); "
單引號中的雙引號
href=' javascript:window.open( " bison.html " ); '
上面兩個式子都可以用
href=( " * ' )
只要用「"」代表第一層的引號,再用「'」代表裡面一層引號就可以了。但是有些限制要注意:第一個限制是,開始跟結尾的引號必須在同一「層」中(在巢狀結構的同一層中)。如:
" some text ' works...
( " some text ' ) works too...
" ( some text | other text) ' even works... but
" ( some text ' ) won't work worth a darn
( " | ) some text ( ' | )sadly, won't work either
另一個限制是,
Another restriction is start and end quotes can't be
nested in the same sub-expression - a matching clause
of...
" something " something else ' end of something ' won't
work
However, you can nest them using a different sub-expression,
like so...
" something ( " something else ' ) end of something
'
It's also worth noting that if no previous double quote
was matched, the single quote just matches a normal
single quote. Still it's safer to use \' to explicitly
check for a single quote if you need to.