If user is entering normal string, I need not to convert it to Xml format.. If I get Html data, then I have to convert into Xml. How can I check that without using Regex?
Asked
Active
Viewed 67 times
0
-
It is hard to be 100% sure if a user enters HTML or plain text, especially using regexp. A better option is probably to first ask the user what he/she will enter, then handle input accordingly. – Stefan Mar 18 '19 at 07:00
-
1yourString.contains("") ? – Ramesh Mar 18 '19 at 07:02
-
User will enter a normal string only, but the text area will convert that into Html. – Sangeet Mar 18 '19 at 07:03
-
https://stackoverflow.com/q/1732348/2506522 "Have you tried using an XML parser instead?" – betontalpfa Mar 18 '19 at 07:11
-
1Java != JavaScript, but you've tagged them both. Which language are you asking about? – Kevin Anderson Mar 18 '19 at 07:12
-
I am asking for both. – Sangeet Mar 18 '19 at 07:46
-
Possible duplicate of [RegEx match open tags except XHTML self-contained tags](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – Sangeet Mar 18 '19 at 09:21
2 Answers
0
Html
and Xml
always Starts with <
Tag. the simplest without regex way of checking if it not Start with <
then it is normal String
. Otherwise If it start with <
then it may be normal String or HTML too.
And Html
contains <html>
Tag.

Khalid Shah
- 3,132
- 3
- 20
- 39
0
Try the following function
function isHTML(str) {
var a = document.createElement('div');
a.innerHTML = str;
for (var c = a.childNodes, i = c.length; i--; ) {
if (c[i].nodeType == 1) return true;
}
return false;
}

chinmayan
- 1,304
- 14
- 13