I have to make a split function in JavaScript -


edit sorry if question wasn't clear here question..

create version of javascript split function, may use indexof , substring help. if give string "heellloolllloolllo" , want remove "llll" function should return "heellloooolllo"

this did far:

function split() {      var entered_string  = document.forms["form1"]["str"].value;     var deleted_char = document.forms["form1"]["char"].value;     var index = entered_string.indexof(deleted_char);     var = deleted_char.length;     var result;     var x ;     (x = 0; x< entered_string.length; x++ )     {         if (index < 0) {             result = entered_string;         } else {             result = entered_string.substring(0, index) +entered_string.substring(index+i);         }     }     alert(result) } 

use replace() function g @ end of regular expression. it's called "global modifier".

var string = 'heellloolllloolllo';    var res = string.replace(/llll/g, '');    console.log(res)

if substring variable need construct new regex object , set g second parameter.

var string = 'heellloolllloolllo';  var find = 'llll';    var regex = new regexp(find,'g');  var res = string.replace(regex, '');    console.log(res)

there other useful modifiers can use:

  • g - global replace. replace instances of matched string in provided text.
  • i - case insensitive replace. replace instances of matched string, ignoring differences in case.
  • m - multi-line replace. regular expression should tested matches on multiple lines.

see this post more information, credit @codejoe.


Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -