porque_no_les_deux
porque_no_les_deux

Reputation: 479

How can I find the position where a certain part of a string begins?

It's late, I'm really tired, I'm having a lot of trouble figuring out a simple thing.

I have a string which will be of the form

BROADCAST FROM x\ny\nz

...where x is a single word with no spaces or newlines, y is a number, and z is a string whose length in characters is y.

All I need is to find the starting index of z so that I may take string.slice(indexOfZ) instead .

What is the form of this regular expression, and how would I write it in JavaScript?

It should be to the effect of...

pattern = /\n[0-9]{1,}\n.*/;
var index = string.match(pattern);

Am I wrong somewhere?

Upvotes: 1

Views: 59

Answers (3)

akonsu
akonsu

Reputation: 29536

Why would /^BROADCAST FROM \w+ \d+ (.+)$/ not work?

Upvotes: 3

Programming Guy
Programming Guy

Reputation: 7441

Regexes are good and all, but in this instance why make it hard for yourself.

var aParts = sFullString.split("\n");

Edit : Looks like u just want z so :

var z = aParts[2];

Upvotes: 1

Anish
Anish

Reputation: 2917

one way to find this is split the string by /

var myString = x\ny\nz;
var str =  myString.split('\'); //if the format is same.

var index = str[2].indexOf("z").

There may be another solution. but this is one that currently comes in my mind.

Hope this one helps.

Upvotes: 0

Related Questions