0

I guess what I need is regular expression.

lets say I have a var containing the following text:

var texty_texy = "Title:<br />Hey there,<br />I'm a simple text.";

How do I determine if there is a "Title:<br />" in my var (ofcourse "Title" could be changed to "Tuna:<br />" or anything) and how do I replace it with:

<div class='title'>Title:</div>Hey there,<br />I'm a simple text.
asked Jun 29, 2014 at 15:09

1 Answer 1

1

You can use groups in your Regular expression by surrounding parts with ().

That means that it will match only if all of parts where found, but also will save results. So /^(\w*)(:)/ will search for Title: and will split in on 1ドル = Title and 2ドル = :<br />. But in case when you need to capture only one part then you can use simplified expression: /^(\w*):/

Then while replacing you can use this variables to add then to resuls by adding 1ドル or 2ドル into text.

var text = "Title:<br />Hey there,<br />I'm a simple text.";
var result = text.replace(/^(\w*):<br \/>/g, "<div class='title'>1ドル:</div>");

Result is:

<div class='title'>Title:</div>Hey there,<br />I'm a simple text.
answered Jun 29, 2014 at 15:13
Sign up to request clarification or add additional context in comments.

6 Comments

Not quite what I wanted. a script to determine if there is something that looks like the pattern of a title ("Text:<br />", "Hi:<br />")
No need to capture the br tag, you can remove the parenthesis surrounding it. But this will work.
but my title doesn't nessaseraly going to be "Title", but the ":<br />" is always going to be there in the var.
That's what I need! - but I got "Unexpected quantifier"
I believe : should have a backslash before it: \:
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.