【问题标题】:Search and replace Using NsRegularExpression使用 NsRegularExpression 搜索和替换
【发布时间】:2013-05-24 01:40:19
【问题描述】:

我正在将字符串中以 px 为单位的字体替换为另一个值。之后,我替换了字符串中的 px,我将它传递给 UIWebView 以显示它。问题是我改了px值之后,就没有效果了。

字符串包含这样的 HTML(例如):

<html><head><script> document.ontouchmove = function(event) { if (document.body.scrollHeight == document.body.clientHeight) event.preventDefault(); } </script><style type='text/css'>* { margin:0; padding:0; } p { color:black; font-family:Helvetica; font-size:26px; } a { color:#000000;}</style></head><body>Look at Google</body></html>

代码如下所示:

//Setting of WebSites Text
NSString *websitesHTML = [[websitesArray objectAtIndex:index] objectForKey:@"Websites"];

//For iPad
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\d\\dpx;" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *modifiedWebsitesHTML = [regex stringByReplacingMatchesInString:websitesHTML options:0 range:NSMakeRange(0, [websitesHTML length]) withTemplate:@"40px"];
NSLog(@"%@",modifiedWebsitesHTML);

UIWebView *websitesWebView = [[UIWebView alloc] init];
[websitesWebView setFrame:CGRectMake(15, 160, 600, 300)];
websitesWebView.delegate = self;
[websitesWebView setOpaque:NO];
websitesWebView.backgroundColor = [UIColor clearColor];
[websitesWebView loadHTMLString:modifiedWebsitesHTML baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
[self.view addSubview:websitesWebView];

任何建议都会有所帮助...需要一些指导...

【问题讨论】:

  • 但是如果替换有效,它与正则表达式有什么关系?这不是网络视图或类似问题的问题吗?

标签: ios uiwebview nsregularexpression


【解决方案1】:

您的正则表达式似乎不匹配,因为在您的 HTML 中“26px”后面没有分号。在 HTML 中添加或从模式中删除。

编辑: 更改后的 CSS 没有效果,因为它设置了 &lt;p&gt; 标签的样式,但 &lt;body&gt; 不包含任何标签。

【讨论】:

  • 这是您在问题中的实际 HTML 吗?您正在设置p 标签的样式,但您的body 中没有p 标签...
猜你喜欢
  • 2019-01-06
  • 1970-01-01
  • 1970-01-01
  • 2013-05-16
  • 2020-07-20
  • 1970-01-01
  • 1970-01-01
  • 2017-01-03
相关资源
最近更新 更多