javascript - How to remove character from string by position number? -
i have string this:
var str = "this **test";
now want remove 2 stars (positions 10
, 11
). , want this:
var newstar = "this test";
again, want remove them using number of position. how can that?
you may use string.replace
also.
> var str = "this **test"; > str.replace(/^(.{10})../, '$1') 'this test'
^(.{10})
captures first 10 characters , following ..
matches 11th , 12th character. replacing matched characters captured chars give expected output.
if want satisfy position condition plus character codition regex must be,
str.replace(/^(.{10})\*\*/, '$1')
this replace 2 stars if placed @ pos 11 , 12.
you may use variable inside regex using regexp
constructor.
var str = "this ***test"; var pos = 10 var num = 3 alert(str.replace(new regexp("^(.{" + pos + "}).{" + num + "}"), '$1'))
Comments
Post a Comment