#!/usr/bin/perl use strict; use warnings; use feature 'say'; use Mojo::DOM; my $html = ' '; my $dom = Mojo::DOM->new( $html ); # find each span tag foreach my $id ( $dom->find('span')->each ){ my $text = $id->text; if ( $text =~ /Spoken/){ $text =~ s/Spoken/Derp!/g; $id->replace( $text ); } } # print the updated DOM say $dom->content;