Daniele
Daniele

Reputation: 847

get text, and split it after a given string

is there any javascript method to get text, and split it after a certain value?

Like for instance, having:

<p>here is some text <!-- read more --> and here is some more</p>

get this:

<p>here is some text</p> <p>and here is some more</p>

I suppose jQuery don't have the tools to achieve this.

Upvotes: 0

Views: 2720

Answers (3)

El Amor Zeewon
El Amor Zeewon

Reputation: 108

var string =my name is zeewon.this is me.
arr=[];
arr=string.split(".");
now you can manipulate the arraay arr which contains arr[0] and arr[1]

Upvotes: 0

Ido Green
Ido Green

Reputation: 2813

Here is a simple example that does what you wish: http://jsfiddle.net/MsFVS/ this is the code:

var str="<p>here is some text <!-- read more --> and here is some more</p>";
str = str.replace("<!-- read more ", "</p>");
str = str.replace("-->", "<p>")
document.write("Our new str:"+str);

I hope it helps.

Upvotes: 0

maxedison
maxedison

Reputation: 17553

JavaScript and jQuery can absolutely achieve this. Working example: http://jsfiddle.net/Brdk2/

Here's the code:

var p = $('p');
var textArray = p.html().split(' <!-- read more --> ');
p.replaceWith('<p>' + textArray.join('</p><p>') + '</p>');​

The .split() and .join() methods above are native JS. All other methods are jQuery. Let me know what questions you have.

Upvotes: 2

Related Questions