JSFiddle - React, Tailwind, and code Playground

by cettox

JavaScript

String.prototype.strim = function(needle){
	var org = this;
	var modified = "";
	var first_pos = 0;
	var last_pos = org.length-1;
	//find first non needle char position
	for(var i = 0; i<org.length;i++){
		if(org.charAt(i) !== needle){
			first_pos = (i == 0? 0:i)
			
			break;
		}
	}
	//find first non needle char position
	for(var i = org.length-1; i>0;i--){
		if(org.charAt(i) !== needle){
			last_pos = (i == org.length? org.length:i+1);
			break;
		}
	}

	return org.substring(first_pos,last_pos);
}

alert("...here..".strim('.'));
alert("..there...".strim('.'))
alert(".their.here.".strim('.'))
alert("hereagain..".strim('.'))