简体   繁体   中英

Find string via regex in JavaScript

I'm getting the following response from ajax GET request ( using $.Ajax )

<div class="clienthoverpage_content">
    <div class="school_info" id="lesson_51123">
        <div class="_desc_content" id="lesson_content">
            <div class="_desc_icon">
            </div>
            <div class="_desc_description">
                <h1 class="hover_name" id="lesson_name"></h1>
                <div class="fraud_warning" id="lesson_fraud_warnings"></div>
                <div class="_desc_class_info" id="lesson_class_info">
                    <div id="lesson_class_name" class="ellipsis"></div>
                    <div id="lesson_type" class=""></div>
                </div>
                <div class="_desc_descriptors" id="lesson_descriptors">
                </div>
                                                    </div>
        </div>
    </div>
</div>
<script type="text/javascript">

    fnInitDisplay = function() {
        UserYou.LoadContexts( g_rgAppContextData );
        BuildHover( 'lesson_51123',  {"id":"123441","class":"51112","student":"506854340"} );
        $('lesson_51123').show();
    }

</script>

The line I am interested in is

BuildHover( 'lesson_51123', {"id":"123441","class":"51112","student":"506854340"} );

I want to take the values of id , class , student . I'm assuming the best way doing this would be using regex but I am failing to find the right regex.

I'm not so great at Regex so I'll give you my example where I just use the indexOf and substring methods. JS-FIDDLE

var str = 'UserYou.LoadContexts( g_rgAppContextData );'+
'BuildHover( "lesson_51123",  {"id":"123441","class":"51112","student":"506854340"} );'+
    '$("lesson_51123").show();';

var _id = '"id":"';
var _class = '","class":"';
var _student = '","student":"';

var classIndex = str.indexOf(_class);
var idIndex = str.indexOf(_id);
var studentIndex = str.indexOf(_student);
var endStr = str.indexOf('"}');

var resultID = str.substr(idIndex + _id.length, classIndex - (idIndex + _id.length) );
var resultClass = str.substr(classIndex + _class.length, studentIndex - (classIndex + _class.length));
var resultStudent = str.substr(studentIndex + _student.length, str.length-2 - (studentIndex + _student.length));
var a = str.match(/BuildHover\(\s*'.*?',\s*(.*?)\s*\)/);
// Note a[1] will be a string use JSON.parse(a[1]) to get an object
console.log(a[1]); // {"id":"123441","class":"51112","student":"506854340"}

Breakdown:

/ # start regex
  BuildHover\( # "BuildHover("
  \s* # 0 or more spaces
  ' # single quote ("'")
  .*?  # everything non gready
  ', # Match "',"
  \s* # 0 or more spacres
  ( # start capture group
    .*? # everything non gready
  ) # end group
  \s* # 0 or more spaces
  \) # ")"
/ # end regex
var string = 'UserYou.LoadContexts( g_rgAppContextData );'+
'BuildHover( "lesson_51123",  {"id":"123441","class":"51112","student":"506854340"} );'+
    '$("lesson_51123").show();';

var jsonString = string.match(/\{"id":.*?\}/);
var jsonObject = JSON.parse(jsonString);

console.log(jsonObject.id);
console.log(jsonObject.class);
console.log(jsonObject.student);

You can find the String in your result and then use JSON.parse to get an object. Even though it's not really clean to do stuff like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM