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?

MD. ABDUL Halim
  • 712
  • 3
  • 11
  • 32

1 Answers1

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

Rias
  • 1,956
  • 22
  • 33
  • Your pattern is selected all string but if i use p instead of test , it will select all p without

    Tag.

    – MD. ABDUL Halim Mar 11 '15 at 11:10
  • Take the selection group as result – Rias Mar 11 '15 at 13:10
  • Actually since HTML tag have to work result string. Now I need content text of HTML tag. You could show me in http://regexr.com which text will be only selected but not HTML Tag. – MD. ABDUL Halim Mar 11 '15 at 15:35
  • 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. – Rias Mar 11 '15 at 16:07
  • 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. – MD. ABDUL Halim Mar 11 '15 at 16:32