search

Home  >  Q&A  >  body text

ios - How to replace both numbers and English in a string with *

I have a string that contains Chinese characters, English and numbers. I want to replace numbers and English with *. How do I do this? Usually it is a simple replacement. This requires regular expressions

PHP中文网PHP中文网2699 days ago4150

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-07-06 10:36:30

    NSString *content = @"哈哈哈哈test哈t123哈哈哈哈";
        
    NSLog(@"替换前--- %@",content);
    
    NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:@"[a-zA-Z0-9]" options:0 error:nil];
    content = [regularExpression stringByReplacingMatchesInString:content options:0 range:NSMakeRange(0, content.length) withTemplate:@"*"];
    
    NSLog(@"替换后--- %@",content);

    reply
    0
  • 欧阳克

    欧阳克2017-07-06 10:36:30

    var str = '123你好呀hehe';
    console.log(str.replace(/\w|\d/gi, '*'))

    reply
    0
  • 巴扎黑

    巴扎黑2017-07-06 10:36:30

    Isn’t this okay?

    var str = '123你好呀hehe';
    console.log(str.replace(/\w/g, '*'));

    reply
    0
  • 迷茫

    迷茫2017-07-06 10:36:30

    Loop it over and judge the ASCII code directly. Use c syntax to compare and change characters one by one

    reply
    0
  • Cancelreply