Looking for assistance with an app that converts text to braille formatting, specifically in handling uppercase letters. The rules are as follows:
- Add a ":" before a single uppercase letter.
:This is an :Example
- Add another ":" before multiple uppercase letters and all caps words.
:This is ::ANOTHER ex::AMple, ::ALRIGHT
- If there are more than three consecutive uppercase words, add "-" at the beginning of the sequence and remove extra "::" within that sequence (except for the last one).
:This is -::A VERY LONG SENTENCE WITH A SEQUENCE OF ALL ::CAPS to serve ::AS ::AN :Example
- When transitioning from uppercase to lowercase within a word (excluding initial capitalization), add ";".
:This is my fin:A;l ::EXAM;ple
I've managed to address some of these rules using regex but struggle with others. Any insights or solutions would be greatly appreciated. Thank you!
// Adds ":" before any uppercase letter
var firstChange = text.replace(/[A-Z]+/g,':$&');
// Adds ":" to double+ uppercase
var secondChange = firstChange.replace(/[([A-Z]{2,}/g,':$&');
// Adds ";" between upper and lower case
var thirdChange = secondChange.replace(/\B[A-Z]+(?=[a-z]/g,'$&;')
Experimented with different approaches while working with regex without much success. Open to suggestions on how to tackle this challenge effectively. Feel free to provide feedback or solutions. Thanks!
Edit: To illustrate all rules combined, here's a final example:
This is an Example. This is ANOTHER exAmple, ALRIGHT? This is A VERY LONG SENTENCE WITH A SEQUENCE OF ALL CAPS to serve AS AN Example. This is my finAl EXAMple.
The transformed version should look like this:
:This is an :Example. :This is ::ANOTHER ex::AM;ple, ::ALRIGHT? :This is -::A VERY LONG SENTENCE WITH A SEQUENCE OF ALL ::CAPS to serve ::AS ::AN :Example. :This is my fin:A;l ::EXAM;ple
SOLVED: Collaborated with @ChrisMaurer and @SaSkY to craft a code solution for the specified problem:
(Edit: Edited fourth change based on input from @Sasky)
var original = document.getElementById("area1");
var another = document.getElementById("area2");
function MyFunction(area1) {
// Include ":" before every uppercase letter
var firstChange = original.value.replace(/[A-Z]+/g, ':$&');
// Add another ":" before multiple uppercase letters
var secondChange = firstChange.replace(/([([A-Z]{2,}|\b[|A-Z]+\b)/g, ':$&');
// Add "-" at the beginning of long uppercase sequences
var thirdChange = secondChange.replace(/\B(::[A-Z]+(\s+::[A-Z]+){3,})/g, '-$&');
// Remove extra "::" within long uppercase sequences
var fourthChange = thirdChange.replace(/(?<=-::[A-Z]+\s(?:::[A-Z]+\s)*)::(?=[A-Z]+\s)(?![A-Z]+\s(?!::[A-Z]+\b))/g, '');
// Add ";" when transitioning from uppercase to lowercase within a word
var fifthChange = fourthChange.replace(/\B[A-Z](?=[a-z])/g, '$&;');
// Update
area2.value = fifthChange;
}
<html>
<body>
<textarea id="area1" rows="4" cols="40" onkeyup="MyFunction()">
</textarea>
<textarea id="area2" rows="4" cols="40"></textarea>
</body>
</html>