R
romayankin
I'm trying to create PHP trim() function equivalent using pure JS code
that truncates spaces from the begging and from the end of the given
string. One of the solutions is to use two "while" loops that will run
through the chars from the beginning and then on the end and cut
spaces. Instead I want to write a RegExp to accomplish this. Here is
the sample to give you an idea of my problem:
var str = new String(" and; n4m ")
var patternTrim = /(^\s+)(.*)(\s+$)/;
var arr = str.match(patternTrim);
Note the space char in the middle of the evaluated string. The
resulting string i want to have looks like this "and; n4m"
that truncates spaces from the begging and from the end of the given
string. One of the solutions is to use two "while" loops that will run
through the chars from the beginning and then on the end and cut
spaces. Instead I want to write a RegExp to accomplish this. Here is
the sample to give you an idea of my problem:
var str = new String(" and; n4m ")
var patternTrim = /(^\s+)(.*)(\s+$)/;
var arr = str.match(patternTrim);
Note the space char in the middle of the evaluated string. The
resulting string i want to have looks like this "and; n4m"