0

I have a string as:

var string = "Digital/ Creative Agencies, Real Estate and other key industries in the market.\r\n\r\nOur Services:\r\n1.Website Design\r\n2.Web Development / Portal Development\r\n3.Content Management Systems (CMS) \r\n4.Mobile Application Development \r\n5.SEO & SMO\r\n6.Online branding\r\n7.Creative works\r\n8.PSD to HTML\r\n\r\nTechnologies:\r\n1. HTML5\r\n2.Joomla\r\n3.PHP5\r\n4.Magento\r\n5.Word Press\r\n6.Drupal\r\n7.Amazon EC2\r\n8.Android & IOS\r\n\r\nWe offer you latest technology and trends. Our Strength is technology, team,timely delivery ofcourse Value for your money."

I want to format it properly asso that the numbered bullets works fine. The resulted string should look like:

...in the market. r\n\r\n
Our Services:
<ol><li>Website Design</li><li>Web Development / Portal Development</li><li>Content Management Systems (CMS) </li><li>Mobile Application Development ... </li><li>PSD to HTML</li></ol>
\r\n\r\nTechnologies:<ol><li>HTML5</li><li>Joomla</li><li>PHP5</li>.....</ol>

So i want to keep \r\n\r\n and just want to replace required \r\n\d. with proper html tags.

I tried extracting the paragraph using var para = string.match(/\r\n\d.*[^\r\n\d.]+\r\n\r\n/g) but it returns last element i.e. 8.PSD to HTML instead of all bullets.

Can you guide what would be good way to replace required tags with html tags?

Should i first try extracting the para using .match() and then replace ? or is it possible to replace them directly using string.replace()

2
  • 1
    You're looking for markdown Commented Jan 15, 2014 at 14:30
  • @thg435 I guess no. I just need to replace the proper tags using javascript regex. What you suggest is good but out of scope for my current project. Commented Jan 15, 2014 at 14:34

1 Answer 1

1

One issue with using replace is that while it is easy to replace the \r\n characters with <li> tags, you have to implement some way of determining where the closing </li> tag goes.

I would do a String.split(), rather than rely on match and replace.

var split = string.split("\\r\\n\\r\\n");

Given the sample string you have listed, this would give you an array with three elements. The middle one contains the data you want for the list items, so re-use the split method to extract out the list elements and iterate over it

var listElements = split[1].split("\\r\\n");
var listString;

for (var i = 0; i < listElements.length; i++){
  listString += "<li>" + listElements[i] + "</li>";
}

$("ol").append(listString);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.