正则表达式更改特定字符串

时间:2016-04-15 13:40:53

标签: javascript regex

我正在处理翻译项目,当用户类型我必须更改某些单词时,如

" yalama" - >" yaLama"

" yalamak" - >" yaLamak"

" kalamadi" - >" kaLamadi"

" salamadi" - >" saLamadi"

我的代码现在运作良好,但我遇到了一个问题:如果我只输入" Lam" ,它将更改为" Lm" 没关系..但如果我输入" aLamsiz" (我的意思是" xxxLamxxx .." )不工作:(



$(document).ready(function(){

$("#ta_1").keyup(function(event) {

var text2 = $(this).val();

text2 = text2.replace(/al/g, "L");

text2 = text2.replace(/([^L]|^)am/g, '$1m');

text2 = text2.replace(/\bLam\b/g, "Lm");
 
$("#ta_1").val(text2);
  
  
});
  
});

<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="utf-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
  
<body>
  
<textarea id="ta_1" rows="5" cols="28" ></textarea>
  
</body>
  
</html>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

这个正则表达式就足够了。

text = text.replace(/a[lL]a/g, "aLa");

答案 1 :(得分:0)

不完全确定您的完整翻译器正在尝试做什么,但要获得原始映射,您只需在第一个正则表达式中添加“a”。

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if !(annotation is CustomPointAnnotation) {
        return nil
    }

    let reuseId = "annotation"

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if anView == nil {
        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        anView!.canShowCallout = true
    }
    else {
        anView!.annotation = annotation
    }

    let cpa = annotation as! CustomPointAnnotation
    var imagePin = UIImage(named:cpa.imageName)
    imagePin = imagePin?.rotateImage(cpa.courseDegrees)
    anView!.image = imagePin

    return anView
}
$(document).ready(function(){

$("#ta_1").keyup(function(event) {

var text2 = $(this).val();

text2 = text2.replace(/al/g, "aL");

text2 = text2.replace(/([^L]|^)am/g, '$1m');

text2 = text2.replace(/\bLam\b/g, "Lm");
 
$("#ta_1").val(text2);
  
  
});
  
});