1

I am trying to use a regular expression to target times and add text to them. But my regular expression has a problem when the input string has line breaks. If I remove the breaks and test the expression, it works.

Also, after I have found targeted times, how do I add text in front of and behind them?

var str = "00:00:01.120
In this lesson, we will learn how to determine the equation of a line, using the available information.

00:00:08.040
Let's look at the first example. Determine the equation of the line, that passes through the points (2, 5), and with the slope of 2. 

00:00:19.000
To begin, we should know that the equation of a line can be written in the form of y = mx + b, where m is the slope, and b is the y-intercept.";


    var patt1 = /(\d\d.\d\d.\d\d.\d\d\d)/gm;
    var result = str.match(patt1);
J0e3gan
  • 8,740
  • 10
  • 53
  • 80
  • 1
    It's a [problem with your Javascript](http://stackoverflow.com/questions/805107/creating-multiline-strings-in-javascript). Strings can't be multiple lines, they must be concatenated with `+` or escape new lines with `\`. – Sam Apr 10 '14 at 02:02
  • 1
    What do you mean by _have break_? Do you mean multiple lines in the string? Javascript doesn't allow you to write a string across multiple lines like that, you should be getting a syntax error. – Barmar Apr 10 '14 at 02:02
  • 1
    Easiest way to create a multi-line string is put a `\` (backslash) at the end of each line, except the last. However, it can cause problems with minifiers and you need to be sure the backslash is the last character of each line. – yitwail Apr 10 '14 at 02:03
  • but it is inside a "" will that have problem ? – user3418336 Apr 10 '14 at 02:05
  • 1
    @user3418336 no, as far as I know `''` and `""` both make the same kind of string in JS. [Edit: confirmed.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) – Sam Apr 10 '14 at 02:06
  • So the string is inside the "" why will i have a syntax error with the line break ? – user3418336 Apr 10 '14 at 02:08
  • I just said no. Please try it out [for yourself](http://jsfiddle.net/CrLB3/). – Sam Apr 10 '14 at 02:10
  • yes , the above sample is what i mean the string is inside the "" , but it is not working – user3418336 Apr 10 '14 at 02:12
  • Oright , i get it i have to add backslash. After i get the result , how do i match them and add some word before and after the result ? TQ – user3418336 Apr 10 '14 at 02:14

2 Answers2

0

As others have noted, you need to first fix your multi-line string.

With this done, you can use the replace method rather than the match method to add text before and after matched times:

var str = "00:00:01.120\n\
In this lesson, we will learn how to determine the equation of a line, using the available information.\n\
\n\
00:00:08.040\n\
Let's look at the first example. Determine the equation of the line, that passes through the points (2, 5), and\ with the slope of 2.\n\
\n\
00:00:19.000\n\
To begin, we should know that the equation of a line can be written in the form of y = mx + b, where m is the\ slope, and b is the y-intercept.";


var patt1 = /(\d\d.\d\d.\d\d.\d\d\d)/gm;
//var result = str.match(patt1);
var result = str.replace(patt1, "<some text>\$1<some other text>");
alert(result);

You can try this in a fiddle.

For more information on the replace method, check out MDN's reference page.

J0e3gan
  • 8,740
  • 10
  • 53
  • 80
0

The way you assign the value for str is incorrect. Your browser must have blocked your script and hence the parsing had never taken place.

You may replace the line breaks with \r\n as follows,

var str = "00:00:01.120\r\nIn this lesson, we will learn how to determine the equation of a line, using the available information.\r\n00:00:08.040\r\nLet's look at the first example. Determine the equation of the line, that passes through the points (2, 5), and with the slope of 2.\r\n00:00:19.000\r\nTo begin, we should know that the equation of a line can be written in the form of y = mx + b, where m is the slope, and b is the y-intercept.";

You can check this fiddle to see how it works.

Johnny
  • 481
  • 4
  • 13