0

My Code is given below,

var str = '<p>(a) test  <span style="text-decoration: line-through;">
test</span>&nbsp;<span style="font-family: arial, helvetica, sans-serif;">
123 test&nbsp;<span style="font-family: SutonnyMJ;"> test test &nbsp;
<span style="background-color: #008000;">test&nbsp;<span style="background-color: #ffffff;">
&nbsp;test <span style="color: #800080;">test</span></span></span></span></span></p>
<table style="height: 43px;" width="250"><tbody><tr><td>test</td>
<td>test</td></tr></tbody></table>';

for(var eq in G){
    var an=new RegExp(eq,"g");
    str=str.replace(an,G[eq]); //For Example an = /t/g and G[eq] = a For One Index 
}

If i use this string without HTML Tag , the result is perfect.

I want to make a regular express pattern -

1. it will not replace any HTML Tag(
Example:<p>,</p> <div>,</div>,&nbsp;,<br/>,<span>,</span>,<table>,</table> and etc) 
and HTML Tag Atrribute(Example : style etc).
2. it will not replace inner/element of HTML Tag(Eexample : span,div etc) if font-fmaily 
is not SutonnyMJ (font-family: SutonnyMJ) in style attribute ;

I assume that test will be abca after replace.

The Result String is given below ,

result_str ='<p>(a) abca <span style="text-decoration: line-through;">
abcd </span>&nbsp;<span style="font-family: arial, helvetica, sans-serif;">
123 test&nbsp;<span style="font-family: SutonnyMJ;"> abca abca &nbsp;
<span style="background-color: #008000;">abca&nbsp;<span style="background-color: #ffffff;">
&nbsp;test <span style="color: #800080;">abca</span></span></span></span></span></p>
<table style="height: 43px;" width="250"><tbody><tr><td>abca</td>
<td>abca</td></tr></tbody></table>';

In result_str 123 test is not change because it's parent style font family is not SutonnyMJ

How to write a JavaScript regular expression pattern. Please any suggestion?

5
  • 1
    Why don't you an HTML parser instead of regex. Commented Mar 11, 2015 at 9:44
  • My Language Converter is working Nince without HTML Tag. But HTML tag have to work in result string. Commented Mar 11, 2015 at 9:49
  • the quotes " are incorrect...... try use single quotes for the string definition... or escape (with backslash) the double-quotes in the string. Commented Mar 11, 2015 at 9:49
  • @JLILI Aman , How to parse using HTML parser. You can say it which i would like to the code. Commented Mar 11, 2015 at 9:56
  • @MD.ABDULHalim stackoverflow.com/questions/10585029/… Commented Mar 11, 2015 at 10:17

1 Answer 1

1

A starting point to the pattern could be this:

(?:<[^\>]>)+(?:[^.]*(test)[^.]*)+(?:<\/[^\>]>)*

Replace test with your string.

**What's missing is the limitation, that the font-family should not be SutonnyMJ. **

Fiddle around and improve it here: http://regexr.com/3aj63

Sign up to request clarification or add additional context in comments.

5 Comments

Your pattern is selected all string but if i use p instead of test , it will select all p without <p> Tag.
Take the selection group as result
Actually since HTML tag have to work result string. Now I need content text of HTML tag. You could show me in regexr.com which text will be only selected but not HTML Tag.
What did you try so far to do that in rexexr? actually the selection group does only select the text. I am not 100% sure about what you want to achieve, the regexr helps you to find a solution by reading the cheatsheet and documentation.
Yes, i can get only text using $(str).text(). But I have to translate one language to another language which if HTML Table/text design(using style attribute) is exist in input string. After converting the string , HTML Table/text design must be shown in the result string. I have JavaScript script for converting/translating language which is converting / translating string letter by letter. If i use $(str).text , i don't get HTML Table/text design in result string. If i get my wanted pattern ,then it is possible. Am i clear to you.

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.