You can achieve this by making an automator workflow and saving it as a service.
Open Automator.app, choose to make a Service.
In the top menu set "Service receives selected" to "text" in "any application".
From the Actions, Library menu search for "Get Value of Variable" and drag to the right in the workflow area. Again in the Actions, Library search for "Run AppleScript" and drag to the workflow area.
In the "Run AppleScript" action enter the flowing code:
on run {input, parameters}
set terminalCommand to item 1 of input
tell application "Terminal"
activate
do script terminalCommand
end tell
return input
end run
Name your variable anything you like. When your done your workflow should look like below. Save it and name it something like "Run in Terminal". Now when your in a web browser you can right click and navigate to Services and you will see your "Run in Terminal" command, which will open up terminal and run the highlighted text.