JavaScript How can i convert a string in to a valid html list using regexp convert string to valid json,convert string to valid datetime

I am working on a angularjs app.

I am receiving the data as an array from back-end. I need to convert that in to a html list. (using li) but how can i do that?

here is the array string i am getting :

"1. Submittals in progress 2. Structural works in progress in S2 & S3 3. Structural works in progress in N4. 4. Childrens Mall pedestal and steel fabrication."

I would like to convert this in to Un-ordered list like this:

<ul>
<li> Submittals in progress </li> //see the number removed.
<li> Structural works in progress in S2 & S3 </li>
<li> Structural works in progress in N4. </li> //end has the dot
<li> Childrens Mall pedestal and steel fabrication. </li>
</ul>
Answer:1
Use simple regex to split the string and then put ng-repeat on the li in html

You can seek help from angular js docs or stack overflow on how to use ng-repeat

var str = "1. Submittals in progress 2. Structural works in progress in S2 & S3 3. Structural works in progress in N4. 4. Childrens Mall pedestal and steel fabrication.";   //sample string

var result = str.split(/^\d+\.+\s+|\s\d\.\s/); //just skip text at index zero
//use ng-repeat and print in whatever way you wanted

result = str.split(/(^\d+\.+\s+|\s\d\.\s)/);  //if numbers are also required
Answer:2

You can use this lookahead based regex:

var s = "1. Submittals in progress 2. Structural works in progress in S2 & S3 3. Structural works in progress in N4. 4. Childrens Mall pedestal and steel fabrication.";

var r = "<ul>\n" +
        s.replace(/\b\d+\.\s+(.+?)\s*(?=\b\d+\. |\s*$)/g, "<li> $1 </li>\n") +
        "</ul>\n";

console.log(r);

Output:

<ul>
<li> Submittals in progress </li>
<li> Structural works in progress in S2 & S3 </li>
<li> Structural works in progress in N4. </li>
<li> Childrens Mall pedestal and steel fabrication. </li>
</ul>

RegEx Demo

RegEx Breakup:

\b\d+              # match 1 or more digits after a word boundary
\.                 # match one literal dot
\s+                # match 1 or more space
(.+?)              # match 1 or more any character and capture it group #1
\s*                # match 0 or more spaces
(?=...)            # a positive lookahead
(?=\b\d+\. |\s*$)  # match must be followed by a word boundary + digits + dot OR
                   # white spaces + line end

Update: To get an array of matched items from original string use String#match as:

var arr = s.match( /\b\d+\.\s+(.+?)(?=\s*\b\d+\. |\s*$)/g );

This will give:

["1. Submittals in progress",
 "2. Structural works in progress in S2 & S3",
 "3. Structural works in progress in N4.",
 "4. Childrens Mall pedestal and steel fabrication."]
Answer:3

I'm aware that this is a topic discussed many times, however I can't find what I'm looking for.. Question: What is the best practice these days as the data source for dynamic drop down boxes, e.g. a ...

I'm aware that this is a topic discussed many times, however I can't find what I'm looking for.. Question: What is the best practice these days as the data source for dynamic drop down boxes, e.g. a ...

  1. html form drop down
  2. html form drop down default
  3. html form drop down default value
  4. html form drop down placeholder
  5. html form drop down list default value
  6. html form drop down multiple select
  7. html form drop down list from database
  8. html form drop down javascript
  9. html form drop down selected value
  10. html form drop down auto submit
  11. html form drop-down list multiple selections
  12. html form drop list
  13. html form drop down default blank
  14. html form drop down example
  15. html form drop box
  16. html form drop
  17. html form drop down checkbox
  18. html form drop down country list
  19. html form drop down button
  20. html form drop down list style

The String object in Javascript has a method called toLowerCase. The problem with that method is that it requires that you already have a String object. What if I wanted to turn that prototype method ...

The String object in Javascript has a method called toLowerCase. The problem with that method is that it requires that you already have a String object. What if I wanted to turn that prototype method ...

Please find this jsfiddle I've created drag n drop no sorting The problem here is that when I put the box from drag-gable area to "drop here" area, the boxes won't be sorted out. For example If I put ...

Please find this jsfiddle I've created drag n drop no sorting The problem here is that when I put the box from drag-gable area to "drop here" area, the boxes won't be sorted out. For example If I put ...

  1. jquery drag drop file upload
  2. jquery drag drop list
  3. jquery drag drop table rows
  4. jquery drag drop example
  5. jquery drag drop file
  6. jquery drag drop image
  7. jquery drag drop event
  8. jquery drag drop plugin
  9. jquery drag drop upload
  10. jquery drag drop order
  11. jquery drag drop clone
  12. jquery drag drop get dropped element
  13. jquery drag drop
  14. jquery drag drop sort
  15. jquery drag drop table
  16. jquery drag drop mobile
  17. jquery drag drop grid
  18. jquery drag drop sortable
  19. jquery drag drop image upload
  20. jquery drag drop list items

I have some replaying functionality working by using Javascript and the window.onload function. However when I go to some other page and then come back to the web application (with the onload ...

I have some replaying functionality working by using Javascript and the window.onload function. However when I go to some other page and then come back to the web application (with the onload ...