re = /(\S*) && (\1[\.\[]).+/m
str = '// Optional chaining for Javascript is now in stage 4:
// https://github.com/tc39/proposal-optional-chaining
// WARNING: This is NOT native to JS yet, so you will need to use a babel plugin to convert it:
// https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining
// Which means you can use this regex to find where in your codebase you can replace this:
user && user.profile && user.profile.email
// with this:
user?.profile?.email
// these should match because they have an object variable being accessed multiple times with a dot or opening bracket
data && data[query] && data[query].items && data[query].items.length > 0
const street = user.address && user.address.street;
// this should not match because they\'re not accessing the same object
data && form.user && api.authenticated'
# Print the match result
str.scan(re) do |match|
puts match.to_s
end
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html