日期:2014-05-16  浏览次数:20466 次

求一个正则表达式

内容:

function geoip_country_code() { return 'CN'; }
function geoip_country_name() { return 'China'; }
function geoip_city() { return 'Wuhan'; }
function geoip_region() { return '12'; }
function geoip_region_name() { return 'Hubei'; }
function geoip_latitude() { return '30.5801'; }
function geoip_longitude() { return '114.2734'; }
function geoip_postal_code() { return ''; }
function geoip_area_code() { return ''; }
function geoip_metro_code() { return ''; }



通过函数名,得到返回值:
如: geoip_country_code 则得到 CN
  geoip_country_name 则得到 China


------解决方案--------------------
有什么意义?
------解决方案--------------------
好奇葩的需求
------解决方案--------------------
这个跟正则扯上什么关系了,不要只是为了正则而用正则
------解决方案--------------------
HTML code

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="gb2312" />
        <title></title>    
        <style>
        </style>        
    </head>
    <body>
        <div id="test">
            function geoip_country_code() { return 'CN'; }
            function geoip_country_name() { return 'China'; }
            function geoip_city() { return 'Wuhan'; }
            function geoip_region() { return '12'; }
            function geoip_region_name() { return 'Hubei'; }
            function geoip_latitude() { return '30.5801'; }
            function geoip_longitude() { return '114.2734'; }
            function geoip_postal_code() { return ''; }
            function geoip_area_code() { return ''; }
            function geoip_metro_code() { return ''; }
        </div>
        <script>
            function $(el){
                return typeof el == 'string' ? document.getElementById(el) : el;
            }
            var str = $('test').innerHTML;
            var json = {};
            var re = /function\s(.+?)\(\).+return\s+'(.*?)'.+?}/gi;
            str = str.replace(re, function($){
                re.test($);
                json[RegExp.$1] = RegExp.$2;
            });
            //console.log(json);
            alert(json.geoip_country_code)
            alert(json.geoip_country_name)
            // 依次。。。
        </script>
    </body>
</html>

------解决方案--------------------
探讨

引用:

好奇葩的需求


请帮我看一下:
http://topic.csdn.net/u/20120412/16/9254b551-a3fd-4e94-9627-0f3d9945a1af.html

------解决方案--------------------
HTML code

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="gb2312" />
        <title></title>    
        <style>
        </style>        
    </head>
    <body>
        <div id="test">
            function geoip_country_code() { return 'CN'; }
            function geoip_country_name() { return 'China'; }
            function geoip_city() { return 'Wuhan'; }
            function geoip_region() { return '12'; }
            function geoip_region_name() { return 'Hubei'; }
            function geoip_latitude() { return '30.5801'; }
            function geoip_longitude() { return '114.2734'; }
            function geoip_postal_code() { return ''; }
            function geoip_area_code() { return ''; }
            function geoip_metro_code() { return ''; }
        </div>
        <script>
            function $(el){
                return typeof el == 'string' ? document.getElementById(el) : el;
            }
            var str = $('test').innerHTML;
            var json = {};
            var re = /f