Это мое решение:
function resolve(path, obj) {
return path.split('.').reduce(function(prev, curr) {
return prev ? prev[curr] : null
}, obj || self)
}
Примеры использования:
resolve("document.body.style.width")
// or
resolve("style.width", document.body)
// or even use array indexes
// (someObject has been defined in the question)
resolve("part.0.size", someObject)
// returns null when intermediate properties are not defined:
resolve('properties.that.do.not.exist', {hello:'world'})
Вы хотите использовать опцию DOTALL
для соответствия друг другу. Из doc.python.org :
re.DOTALL
Сделайте '.' специальный символ соответствует любому персонажу вообще, включая новую строку; без этого флага ". будет соответствовать чему угодно, кроме новой строки.
blockquote>Демо:
In [1]: import re In [2]: s="""shantaram is an amazing novel. It is one of the best novels i have read. the novel is written by gregory david roberts. He is an australian""" In [3]: print re.findall('^.*?the',s,re.DOTALL)[0] shantaram is an amazing novel. It is one of the
Используйте это регулярное выражение,
re.search("shantaram[\s\S]*?the", string)
вместо
re.search("shantaram[\s\S]*the", string)
Единственное отличие - «?». Используя «?» (Например, * ?, +?), Вы можете предотвратить самое длинное совпадение.