How to remove specific character surrounding a string? How to remove specific character surrounding a string? php php

How to remove specific character surrounding a string?


Search for character mask and return the rest without.

This proposal the use of the bitwise not ~ operator for checking.

~ is a bitwise not operator. It is perfect for use with indexOf(), because indexOf returns if found the index 0 ... n and if not -1:

value  ~value   boolean -1  =>   0  =>  false  0  =>  -1  =>  true  1  =>  -2  =>  true  2  =>  -3  =>  true  and so on 

function trim(s, mask) {    while (~mask.indexOf(s[0])) {        s = s.slice(1);    }    while (~mask.indexOf(s[s.length - 1])) {        s = s.slice(0, -1);    }    return s;}console.log(trim('??? this is a ? test ?', '? '));console.log(trim('abc this is a ? test abc', 'cba '));