mnb93
asked on
preg_match preg_replace in javascript.
Basically I would like to know how to do these PHP things:
preg_match preg_replace
and
$x = str_split($x, 1);
$x = array_unique($x);
$x = implode('',$x);
in javascript.
preg_match preg_replace
and
$x = str_split($x, 1);
$x = array_unique($x);
$x = implode('',$x);
in javascript.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
var x = "This is a sentence";
x.replace("is","was");
if(x.indexOf("sentence") != -1)
{
alert("We found a match");
}
x.replace("is","was");
if(x.indexOf("sentence") != -1)
{
alert("We found a match");
}
BTW, an alternative for "preg_match" would be
/RegExp/.exec ("string");
This does the same thing as "string".match ( /RegExp/ );
/RegExp/.exec ("string");
This does the same thing as "string".match ( /RegExp/ );
ASKER
var x = "some_long_string";
x = x.split('_', 1);
But $x = str_split($x, 1);
Means to put each char into an array.
x = x.split('_', 1);
But $x = str_split($x, 1);
Means to put each char into an array.
ASKER
v.match (/^\s|\s$/)
doesn't work.
doesn't work.
result_array = v.match ( /^\s|\s$/ );
see also http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:String:match .
see also http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:String:match .
ASKER
I wanted to use it in an if?
ASKER
eg.
if(v.match ('/^\s|\s$/'))
{
//
}
if(v.match ('/^\s|\s$/'))
{
//
}
ASKER
well without the '
ASKER
And for this: /[^A-Za-z0-9\'"|_\[\]\- ]/ where I am escaping the ' do I need to do that?
ASKER
Ok I am all good except:
var x = "some_long_string";
x = x.split('_', 1);
But $x = str_split($x, 1);
Means to put each char into an array.
var x = "some_long_string";
x = x.split('_', 1);
But $x = str_split($x, 1);
Means to put each char into an array.
1)
if( /^\s|\s$/.test(v) ) {
...
}
There should be no 'quotes' around the slashes (/ ... /). In JS regular expressions are native objects so you don't need to quote them around '...'.
2)
You do not need to escape the ', because it isn't special in RegExp.
/[^A-Za-z0-9'"|_\[\]\- ]/
3)
Use
x = x.split ( /\B|\b/ );
if( /^\s|\s$/.test(v) ) {
...
}
There should be no 'quotes' around the slashes (/ ... /). In JS regular expressions are native objects so you don't need to quote them around '...'.
2)
You do not need to escape the ', because it isn't special in RegExp.
/[^A-Za-z0-9'"|_\[\]\- ]/
3)
Use
x = x.split ( /\B|\b/ );
ASKER
array_unique has no equivalent you could use sort and the remove second appearanve of a data
i/e/
a = new Array("X" ,"y" ,"d", "Z", "v","m","r");
a = a.sort();
then use for(...) to compare....
How do I compare it?
i/e/
a = new Array("X" ,"y" ,"d", "Z", "v","m","r");
a = a.sort();
then use for(...) to compare....
How do I compare it?
ASKER
function array_unique(arr) {
var newArray = [];
var existingItems = {};
var prefix = String(Math.random() * 9e9);
for (var ii = 0; ii < arr.length; ++ii) {
if (!existingItems[prefix + arr[ii]]) {
newArray.push(arr[ii]);
existingItems[prefix + arr[ii]] = true;
}
}
return newArray;
}
var newArray = [];
var existingItems = {};
var prefix = String(Math.random() * 9e9);
for (var ii = 0; ii < arr.length; ++ii) {
if (!existingItems[prefix + arr[ii]]) {
newArray.push(arr[ii]);
existingItems[prefix + arr[ii]] = true;
}
}
return newArray;
}
ASKER