Cam
Cam

Reputation: 1902

Regex remove content before >

Trying to create a Regex that removes ABC before a >. For example.

Agencies > Freelancers > Driving Instructors

Anything before > is removed. Leaving only Driving Instrucor.

So far I can remove non-alphanumeric

[^a-zA-Z0-9 -]

I also tried

.*?(?=[^a-zA-Z0-9 -]) 

But that only Removes Agencies.

The point of this is,

To first get the first word.

Agencies.

Store that into a json object. Then, using .match grab any other item that is preceded with "Agencies" as its children.

Thereby I should have this.

{"Agencies":[
   {"link":"Freelancers "}, 
   {"link":"Driving Instructors"},
]}

I'm not concerned about the jSon, but I need to iterate through first.

thank you.

Upvotes: 0

Views: 96

Answers (1)

lpg
lpg

Reputation: 4937

Which language? In php, maybe with "explode" you have enough:

<?
$string = "Agencies > Freelancers > Driving Instructors";
$exploded = explode(">",$string);
$key = trim(array_shift($exploded));
$array = array($key=>array());
foreach($exploded as $subitem)
{
        $array[$key][] = array("link"=>$subitem);
}
echo json_encode($array);
?>

Using javascript could be something like this:

var string = "Agencies > Freelancers > Driving Instructors";
var exploded = string.split(">");
var key = exploded[0].trim();
var values = new Array();
for(i=1;i<exploded.length;i++)
{
    values.push({link:exploded[i].trim()});
}
var json = "{\"" + key + "\": " + JSON.stringify(values) +"}";

Upvotes: 2

Related Questions