jQuery or JavaScript equivalent of PHP strpos function to find string on a page

前端 未结 2 719
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-01 09:05

Is there an equivalent function in JavaScript or jQuery similar to strpos in PHP?

I want to locate a string inside an element on a page. The string I\'m

2条回答
  •  甜味超标
    2021-01-01 09:24

    You don't need jquery for this -- plain old Javascript will do just fine, using the .indexof() method.

    However if you really want an exact syntax match for PHP's strpos(), something like this would do it:

    function strpos (haystack, needle, offset) {
      var i = (haystack+'').indexOf(needle, (offset || 0));
      return i === -1 ? false : i;
    }
    

    Note: This function taken from here: http://phpjs.org/functions/strpos:545

    JSFiddle

提交回复
热议问题