JavaScript: can I search the regular expression not from the start of string?

365
May 09, 2017, at 08:20 AM

I learned that indexOf() could not be used for searching the regular expression in the string, however search() has not the start position and the end position as the optional parameters. How can I find and replace all certain regular expression in the same string? I added the problem where it is no so simple as replace() will be enough.

Problem example

  1. Replace all consecutive two <br/><br/> with </p><p>, if after second <br/> some letters or digits (\w) are following.
  2. Leave all single <br/> of three or more consecutive <br/> such as.
  3. If there are no letter or digits after consecutive two <br/><br/>, leave it such as.

If we use replace() for solving this problem, not only <br/><br/>, but also following symbols will be replaced. To evade it, we need:

  1. Find the start of matching with regular expression. It will be /(?:<br\s*[\/]?>){2}\s*\w+/.
  2. From the start of matching position, find the start position of \w part.
  3. Replace the /(?:<br\s*[\/]?>){2}\s*/ part with </p><p>.
  4. Repeat 1-3 inside the loop from the end of the previous matching position util next matches exists.

As I told above, I don't know how to search the new matching from the certain position. Is there some ways except slice the string and join it again?

var testString = $('#container').html(); 
console.log(testString.search(/(?:<br\s*[\/]?>){2}\s*\w+/));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<div id="container"> 
  <p>  
    <!-- Only one br: leave such as -->	 
    Brick quiz whangs jumpy veldt fox! <br/> 
 
    <!-- Two br and letters then: replace by </p><p> -->	 
    Sphinx of black quartz judge my vow! <br/><br /> 
 
    <!-- No symbols after 2nd br: leave such as -->	 
    Pack my box with five dozen liquor jugs. <br/><br /><br/> 
 
    <!-- Two br and symbols then: replace by </p><p> -->	 
    The vixen jumped quickly on her foe barking with zeal. <br/><br /> 
 
    <!-- No letters after <br/><br/>: leave such as -->	 
    Brawny gods just flocked up to quiz and vex him.<br/><br /> 
  <p> 
</div>

Rent Charter Buses Company
READ ALSO
How to add values from two different functions?

How to add values from two different functions?

In my case, I found the value of 2 different sums in two different functionsNow all I need to do is subtract these two values but I am not sure if it is possible to do so from two different functions

334
Where i can save the data of user on Frontend with a token jwt system based

Where i can save the data of user on Frontend with a token jwt system based

I need save data of user when user login with tokenHow i can save data of user for example her user id , her name , her x data , i think save data in local storage but is it insecure because this data any person can change on the console

280