JavaScript match vs trim

Both the match and the trim can be used as a substitute to string.contains() since JavaScript does not contain the property contains, however they work in different ways:

Match

docomunet.getElementById(‘myId’).textContent.match(‘stringToLookFor’);   // FireFox:  looks for the content ‘stringToLookFor’ in ‘myId’ text and returns true or false

docomunet.getElementById(‘myId’).innerText.match(‘stringToLookFor’);   // Chrome/IE:  looks for the content ‘stringToLookFor’ in ‘myId’ text and returns true or false

Trim

docomunet.getElementById(‘myId’).textContent.trim(‘stringToLookFor’);   // FireFox:  trims the text in myId to ‘stringToLookFor’ and is returned as string /var

docomunet.getElementById(‘myId’).innerText.trim(‘stringToLookFor’);   // Chrome/IE:   trims the text in myId to ‘stringToLookFor’ and is returned as string /var

To use trim as a contains property we have to add:

docomunet.getElementById(‘myId’).textContent.trim(‘stringToLookFor’) == ”stringToLookFor’       //Returns true or false

A typical example of match usage:

if(docomunet.getElementById(‘myId’).textContent.match(‘stringToLookFor’))   // FireFox: used as a condition

if(docomunet.getElementById(‘myId’).innerText.match(‘stringToLookFor’))   // Chrome/IE:  used as a condition

A typical example of trim usage:

docomunet.getElementById(‘myId’).textContent = docomunet.getElementById(‘myId’).textContent.trim(‘stringToLookFor’);   // FireFox: sets the text in myId to the trimmed string

docomunet.getElementById(‘myId’).innerText = docomunet.getElementById(‘myId’).innerText.trim(‘stringToLookFor’);   // Chrome/IE: sets the text in myId to the trimmed string

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s