Reputation: 51
Here is an excerpt from the html code:
...
<body>
<textarea name="user_request" id ="textrequest" cols="50" rows="5">
Drei Chinesen mit dem Kontrabass
saßen auf der Straße und erzählten sich was.
Da kam die Polizei, ei, was ist denn das?
Drei Chinesen mit dem Kontrabass.
</textarea>
<p></p>
<select name="vowel" id="vowel" size="1">
<option>a</option>
<option>e</option>
<option>i</option>
<option>o</option>
<option>u</option>
<option>ä</option>
<option>ö</option>
<option>ü</option>
</select>
<button onclick="replace();">Replace</button>
<div id="output" style="font-size: 12">
</div>
</body>
...
jscript part:
function replace() {
var textrequest = document.getElementById("textrequest").value;
var selection = document.getElementById("vowel").value;
var result = textrequest.replace(/ä/,"ae");
var output = result.replace(/[aeiouöüä]+/g,selection);
document.getElementById("output").innerHTML=output;
}
What I want to do with this program is to change every vowel into an "a" for example like this:
Draa Chanasan mat dam Kantrabass Saßan aaf dar Straßa and arzahlten sach was. Da kam daa Palazaa, aa, was ast dann das? Draa Chinasan mat dam Kantrabass.
It works pretty good on most part, but it does not change "ei" into "aa". It only changes the first vowel like: Drei into Dra instead of Drei into Draa. I hope my problem is somewhat understandable. Many thanks in advance!
Upvotes: 0
Views: 97
Reputation: 1402
This is what I came up with:
function replace() {
var textrequest = document.getElementById("textrequest").value;
var selection = document.getElementById("vocal").value;
var parse = textrequest.replace(/[aeiouöüä]/g, selection);
var result = "";
var previous = "";
for(var i = 0; i < parse.length; i++)
{
var char = parse[i];
if(char != previous)
{
result += char;
previous = char;
}
else
{
switch(char)
{
case "a":
case "e":
case "i":
case "o":
case "u":
case "ä":
case "ö":
case "ü":
break;
default:
result += char;
previous = char;
break;
}
}
}
document.getElementById("textrequest").value = result;
}
http://codepen.io/anon/pen/mJWQEQ
I'm not sure if it is correct behavior. I did this from totaly different direction which may help you.
EDIT 1: Added the forgotten case 'o':
and removed parsing for ä.
EDIT 2: Fixed the duplicates.
EDIT 3: Fixed the order of operations.
Upvotes: 1
Reputation: 3062
the problem is the regex [aeiouöüä]+
the +
in the end means "one or more consecutive matches"
so in Drei
the match will be the consecutive vowels Dr>ei<
and the match (all vowels) will replaced by a
to replace letter by letter you should use
var output = result.replace(/[aeiouöüä]/g,selection);
Upvotes: 0