substring selector with jquery?

后端 未结 5 1144
南笙
南笙 2021-01-23 01:02

Is it possible to select only part of a string using jquery? For example I have a text

Metuentes igitur idem latrones Lycaoniam magna parte campestrem&l

5条回答
  •  星月不相逢
    2021-01-23 01:21

    There is no way to do this with pure jQuery. You could do this instead :

    var search = 'Lycaoniam';
    var orig = $('p').html();
    var highlighted = orig.replace(new RegExp(
        search.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'gi'
    ), '$&');
    $('p').html(highlighted);
    

    To revert back to the original text :

    $('p').html(orig);
    

    The search.replace(...) part allows to deal with special chars : http://jsfiddle.net/wared/TPg9p/.

提交回复
热议问题