skip to Main Content

Hi I went through many answers on this but couldn’t find a similar one which worked. Kindly help with this.
I have a script which hides mobile number inside <p></p> tag, the present script hides any numbers added. We want this to be like it should hide numbers which are above 9 digits only. And also if it has
<span></span> tag it should not hide the mobile number. Can we achieve this please.

We want it like this:

<p>Hide 1234567890<span>Show 0987654321</span></p>

Output will be: ‘Hide *** Show 0987654321’

<script>
$(document).ready(function() {
  $('#hidenumber').find('p').text(function(i,txt) {
    return txt.replace(/[1-9]+/,'***');
  });
});
</script>

3

Answers


  1. Try it like this:

    $(document).ready(function () {
            $('#hidenumber').find('p').text(function (i, txt) {
                // if <p> does not contain <span>
                if ($(this).find('span').length == 0) {
                    return txt.replace(/d{9,}/g, '***');
                }
            });
        });
    
    Login or Signup to reply.
  2. $(document).ready(function() {
      $('#hidenumber p').each(function() {
        var $paragraph = $(this);
        
        // Traverse through the text nodes and apply the replacement
        $paragraph.contents().each(function() {
          if (this.nodeType === Node.TEXT_NODE) {
            var originalText = this.nodeValue;
            var modifiedText = originalText.replace(/bd{10,}b/g, '***');
            this.nodeValue = modifiedText;
          }
        });
      });
    });
    

    In this updated script, the each function is used to iterate over each

    element within the #hidenumber container. For each paragraph, it checks if there are any tags inside. If there are no tags, it applies the regular expression bd{10,}b to replace numbers with 10 or more digits with ***. The modified text is then set back as the content of the paragraph using the html function.

    Please note that this script assumes that the container with the ID hidenumber contains the

    elements you want to modify. Make sure to adjust the selector (#hidenumber) according to your HTML structure.

    With this modification, the script will only hide numbers above 9 digits and exclude numbers within tags.

    Login or Signup to reply.
  3. Try this:

    Using the Node.TEXT_NODE will exclude text/numbers in the other elements such as span.

    <html>
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hide span numbers</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $("#hidenumber").contents().each(function() {
                if (this.nodeType === Node.TEXT_NODE) {
                    $(this).replaceWith(this.nodeValue.replace(/bd{10,}b/g, '***'));
                }
            });
        });
    </script>
    </head>
    <body>
    <p id="hidenumber">
    Hide 1234567890 <span>Show 0987654321</span><br>
    Hide: 1234567890234 <span>Show 0987654321</span><br>
    Show: 123456789 <span>Show: 0987654321</span><br>
    Show: 1234567 <span>Show: 0987654321</span><br>
    Hide: 1234567899 <span>Show: 123451234512345</span><br>
    Hide: 1234567899 Hide: 123451234512345
    </p>
    </body>
    </html>
    

    Adam (APB Reports)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search