Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Insert html into a string

Posted on 2007-12-02
5
Medium Priority
?
774 Views
Last Modified: 2012-06-27
Hello there ..
I am not sure how to go about this regex problem. I need to insert <strong> ... </strong> tags into a string at a specific point eg:
Every string has the following pattern:  "You want to make a change! You think about y... "  - a string of words until the "!" and then further text.
I need to insert the <strong> tag  at the start of the string and then </strong> after the "!" character. Is there a way to do this in with one pass over the whole string, returning the desired result ?
Thanks
(using vb.net 2003)
0
Comment
Question by:simshp
5 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20392742
you can do it without regex, something like:

Dim s as String  = "You want to make a change! You think about y... "
s = s.Insert(IndexOf("!", s)+1, "</strong>").Insert(0, "<strong>")

but always you have to check if the ! is present, first:
If (s.Contains("!") Then s = s.Insert(IndexOf("!", s)+1, "</strong>").Insert(0, "<strong>")

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 20392759
First problem is that you have to define what characters belong to the sentance ending with the exclamation mark. My first guess would be: all lovercase, uppercase characters, blanks, coma, dash, but excluding dot, xclamation char and quota characters. So you see it is diffecult to make it 100 percent matching.

0
 
LVL 17

Expert Comment

by:ZeonFlash
ID: 20392785
Unfortunatley I'm not at an environment that I can test this with, but I think the following regex should get you close to what you want.  You can use it to find all sentences that begin with an upper case character and end with an exclamation point.  You should be able to use it in conjunction with the Regex.Replace.
.*[A-Z]+.*!

Open in new window

0
 
LVL 63

Accepted Solution

by:
Zvonko earned 750 total points
ID: 20392788
Here some example in JavaScript:
<script>
 
var myStr = 'Hello there: You want to make a change! You think about y... "  - a string of words until the "!" and then further text. Here more example!';
 
var newStr = myStr.replace(/([A-Z][a-zA-Z\s\,\-]+!)/g,"<strong>$1</strong>");
alert(newStr)
</script>

Open in new window

0
 

Author Closing Comment

by:simshp
ID: 31412230
Thanks, I based my solution on this answer
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question