[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 10769
  • Last Modified:

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.
0
mnb93
Asked:
mnb93
2 Solutions
 
Ivo StoykovCommented:
Hello mnb93,
var x = "some_long_string";
x = x.split('_', 1);

implode('',$x); is

var x = array_variable_name.join(''); or
var x = array_variable_name.join(', ');

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....

HTH

I
0
 
mnb93Author Commented:
What about: preg_match preg_replace?
0
 
KennyTMCommented:
"string".match ( /RegExp/ );

and

"string".replace ( /RegExp/ , "replace_str");
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
REA_ANDREWCommented:
var x = "This is a sentence";
x.replace("is","was");

if(x.indexOf("sentence") != -1)
{
alert("We found a match");
}
0
 
KennyTMCommented:
BTW, an alternative for "preg_match" would be

/RegExp/.exec ("string");

This does the same thing as "string".match ( /RegExp/ );
0
 
mnb93Author Commented:
var x = "some_long_string";
x = x.split('_', 1);

But $x = str_split($x, 1);

Means to put each char into an array.
0
 
mnb93Author Commented:
v.match (/^\s|\s$/)
doesn't work.
0
 
KennyTMCommented:
0
 
mnb93Author Commented:
I wanted to use it in an if?
0
 
mnb93Author Commented:
eg.

    if(v.match ('/^\s|\s$/'))
    {
//
}
0
 
mnb93Author Commented:
well without the '
0
 
mnb93Author Commented:
And for this: /[^A-Za-z0-9\'"|_\[\]\- ]/ where I am escaping the ' do I need to do that?
0
 
mnb93Author Commented:
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.
0
 
KennyTMCommented:
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/ );
0
 
mnb93Author Commented:
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?
0
 
mnb93Author Commented:
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;
}
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now