开发者

How can I calculate the difference in text blocks via JavaScript / jQuery?

I have 2 blocks of text.. I want 开发者_JS百科to see what the difference percentage is between them. How can I do this?


That depends, what is your definition of difference? Do you want to use the levenshtein distance or something else?

You can find some implementations of the levenshtein algorithm in javascript here and here. You could convert the outcome of the levenshtein distance to a percentage with the following calculation:

percentage = 100 * (levenshtein distance) / (total number of characters)


Well getting the content of the text boxes is easy, just use the $.html (or $.val for form controls like textarea elements) function.

For comparing the strings, you'll need to use a string analysis algorithm like Levenshtein Distance to work out how different the strings are, and then use the length of the blocks of text to work out a percentage.

Hopefully that will give you a starting point.


PHP's similar_text() can do this, and phpJS has created the JS port of it.

See also: levenshtein() and soundex()


Depends what you're differentiating. http://fiddle.jshell.net/G9ghG/2/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜