Home >Web Front-end >JS Tutorial >What are the Best String Replacement Techniques in JavaScript?
Beyond what you wrote above, there is not much that can be win over String#replace() as it is already a built-in function. If what you need is something more drastic, you will likely want to use .split() to transform the original string into an array, modify the array elements, and then .join() them back into a string.
A function to replace all accented characters with their non-accented equivalent is a very niche operation that is unlikely to be very useful to someone else, so it will not be foolhardy to build your own instead of looking it up somewhere or using a library.
String#localCompare() is a utility function for doing a locale-aware comparison of two strings. Depending on the browser's language settings, this would do what you want to do without any transformation:
> 'ä'.localCompare('a') -1 > 'ä'.localCompare('ä') 0 > 'a'.localCompare('ä') 1
This is especially useful when you want to call the user's locale-aware sort() function, available in the majority of the modern web browsers:
> var arr = ['ä','c','b']; > arr.sort(function(a,b){return a.localeCompare(b);}); > console.log(arr); ["a", "b", "c"]
The above is the detailed content of What are the Best String Replacement Techniques in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!